Node response.writeHead() Method
Last Updated :
14 Jan, 2024
The `response.writeHead()` property, introduced in Node.js v1.0, is part of the ‘http‘ module. It is used to send a response header to the incoming request. The status code represents a 3-digit HTTP status code (e.g., 404), and the headers parameter contains the response headers. Optionally, a human-readable statusMessage can be provided as the second argument.
When using `response.setHeader()`, headers set with it will merge with those passed to `response.setHeader()`, where the latter takes precedence. If `response.writeHead()` is called without prior use of `response.setHeader()`, it directly writes supplied headers to the network channel, bypassing internal caching. This can affect the expected result of `response.getHeader()`. For progressive header population with future retrieval and modification, prefer using `response.setHeader()`.
In order to get a response and a proper result, we need to import ‘http’ module.
Syntax to Import:
const http = require('http');
Syntax:
response.writeHead(statusCode[, statusMessage][, headers]);
Parameters: It accepts three parameters as mentioned above and described below:
- statusCode <number>: It accepts the status codes that are of number type.
- statusMessage <string>: It accepts any string that shows the status message.
- headers <Object>: It accepts any function, array, or string.
Return Value <http.ServerResponse>: It returns a reference to the ServerResponse, so that calls can be chained.
Example 1: The below example illustrates the use of response.writeHead() property in Node.js.
Javascript
var http = require( 'http' );
const PORT = process.env.PORT || 3000;
var httpServer = http.createServer(
function (request, response) {
const body = 'hello world' ;
response.writeHead(200,
{
'Content-Length' :
Buffer.byteLength(body),
'Content-Type' :
'text/plain'
});
response.end(body);
});
httpServer.listen(PORT,
() => {
console.log( "Server is running at port 3000..." );
});
|
Step to run index.js file using the following command:
node index.js
Console Output:
Server is running at port 3000...
Example 2: The below example illustrates the use of response.writeHead() property in Node.js.
Javascript
var http = require( 'http' );
const PORT = process.env.PORT || 3000;
var httpServer = http.createServer(
function (request, response) {
response.setHeader( 'Content-Type' , 'text/html' );
response.setHeader(
'Set-Cookie' ,
[
'type=ninja' ,
'language=javascript'
]);
response.setHeader( 'X-Foo' , 'bar' );
response.writeHead(200,
{
'Content-Type' : 'text/plain'
});
const headers = response.getHeaders();
console.log(headers);
response.end( 'ok' );
});
httpServer.listen(PORT, () => {
console.log(
"Server is running at port 3000..." );
});
|
Step to run index.js file using the following command:
node index.js
Output:
Server is running at port 3000...
[Object: null prototype] {
'content-type': 'text/plain',
'set-cookie': [ 'type=ninja', 'language=javascript' ],
'x-foo': 'bar'}
We have a complete list of Express.js Response methods, properties and events, to check those please go through this Express.js Response Complete Reference article.
Share your thoughts in the comments
Please Login to comment...