Node response.writeHead() Method
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
// Node.js program to demonstrate the // response.writeHead() Method // Importing http module var http = require( 'http' ); // Setting up PORT const PORT = process.env.PORT || 3000; // Creating http Server var httpServer = http.createServer( function (request, response) { const body = 'hello world' ; // Calling response.writeHead method response.writeHead(200, { 'Content-Length' : Buffer.byteLength(body), 'Content-Type' : 'text/plain' }); response.end(body); }); // Listening to http Server 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
// Node.js program to demonstrate the // response.writeHead() Method // Importing http module var http = require( 'http' ); // Setting up PORT const PORT = process.env.PORT || 3000; // Creating http Server var httpServer = http.createServer( function (request, response) { // Setting up Headers response.setHeader( 'Content-Type' , 'text/html' ); response.setHeader( 'Set-Cookie' , [ 'type=ninja' , 'language=javascript' ]); response.setHeader( 'X-Foo' , 'bar' ); // Calling response.writeHead method response.writeHead(200, { 'Content-Type' : 'text/plain' }); // Getting the set Headers const headers = response.getHeaders(); // Printing those headers console.log(headers); // Prints Output on the // browser in response response.end( 'ok' ); }); // Listening to http Server 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.