node js response header
Node.js Response Header
To set a response header in Node.js, you can use the setHeader
method of the response object. Here are the steps to achieve this:
- Access the Response Object:
In your Node.js application, when handling a request, you have access to the response object. You can use this object to set the response header.
Set the Header:
Use the
setHeader
method of the response object to set the desired header. For example, to set the "Content-Type" header to "application/json", you can use:javascript response.setHeader('Content-Type', 'application/json');
Send the Response:
- After setting the header, you can send the response using the
end
method of the response object.
By following these steps, you can effectively set the response header in your Node.js application.