Open In App

Node.js response.write() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The response.write() (Added in v0.1.29) method is an inbuilt Application program Interface of the ‘http’ module which sends a chunk of the response body that is omitted when the request is a HEAD request. If this method is called and response.writeHead() has not been called, it will switch to implicit header mode and flush the implicit headers.

The first time response.write() is called, it will send the buffered header information and the first chunk of the body to the client. The second time response.write() is called, Node.js assumes data will be streamed and sends the new data separately. That is, the response is buffered up to the first chunk of the body. A chunk can be a string or a buffer. If the chunk is a string, the second parameter specifies how to encode it into a byte stream. And the callback will be called when this chunk of data is flushed.

In order to get a response and a proper result, we need to import ‘http’ module.

Import:

const http = require('http');

Syntax:

response.write(chunk[, encoding][, callback]);

Parameters: This method accepts three parameters as mentioned above and described below:

  • chunk <string> | <Buffer>: It accepts any Buffer, or String Data.
  • encoding <string>: The default encoding set is ‘utf8‘. It accepts String Data.
  • callback <Function>: It accepts a callback function.

Return Value <Boolean>: It returns true if the entire data was flushed successfully to the kernel buffer and returns false if all or part of the data was queued in user memory. The ‘drain‘ will be emitted when the buffer is free again.

The below example illustrates the use of response.write() property in Node.js.

Example 1: Filename: index.js




// Node.js program to demonstrate the 
// response.write() 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){
  
  // Writing string data
  response.write("Heyy geeksforgeeks ", 'utf8', () => {
      console.log("Writing string Data...");
  });
  
  // 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...");
});


Output:

Output: In-Console

Server is running at port 3000…

Writing string Data…

Now run http://localhost:3000/ in the browser.

Output: In-Browser

Heyy geeksforgeeks  ok

Example 2: Filename: index.js




// Node.js program to demonstrate the 
// response.write() 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){
  
  var str = "GeeksForGeeks wishes you a warm welcome...";
  
  // Writing string data with
  // 16-bit Unicode Transformation Format
  response.write(str, 'utf16', () => {
     console.log("Writing string Data...");
  });
  
  // Allocating predefined Buffer 'Hello world'
  const buf = Buffer.alloc(11, 'aGVsbG8gd29ybGQ=', 'base64');
  
  // Writing the buffer data.
  response.write(buf, 'utf8', () => {
     console.log("Writing Buffer Data...");
  });
  
  // Creating buffer
  const buff = Buffer.from(' hello world', 'utf8');
  
  // Writing the buffer data.
  response.write(buff, 'utf8', () => {
     console.log("Writing Buffer Data...");
  });
  
  // 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...");
});


Run index.js file using the following command:

node index.js

Output:

Output: In-Console

Server is running at port 3000…

Writing string Data…

Writing Buffer Data…

Writing Buffer Data…

Now run http://localhost:3000/ in the browser.

Output: In-Browser

GeeksForGeeks wishes you a warm welcome…hello world hello world ok

Reference: https://nodejs.org/api/http.html#http_response_write_chunk_encoding_callback



Last Updated : 23 Sep, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads