Node.js response.write() Method
Last Updated :
23 Sep, 2020
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
javascript
// 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
javascript
// 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
Similar Reads
Node.js response.writeContinue() Method The response.writeContinue() (Added in v0.3.0) method is an inbuilt Application Programming Interface of the âhttpâ module which sends an HTTP/1.1 100 Continue message to the client, indicating that the request body should be sent. See the 'checkContinue' event on Server. The response.writeContinue(
2 min read
Node response.writeHead() Method The response.writeHead() property is used to send a response header to the incoming request. It was introduced in Node.js v1.0 and is a part of the 'http' module. The status code represents a 3-digit HTTP status code (e.g., 404), and the headers parameter contains the response headers. Optionally, a
3 min read
Node.js stringDecoder.write() Method The stringDecoder.write() method is used to return a decoded string from the given Buffer, TypedArray or DataView. This method also ensures that any incomplete multibyte characters at the end of the buffer are omitted from the returned string. These characters are stored in an internal buffer for th
2 min read
Node response.setHeader() Method The response.setHeader() method in Node.js is used to set HTTP headers for a response. It allows you to define one or more headers for the HTTP response sent by the server. If a header with the same name already exists, its value will be replaced. Additionally, headers can be set as an array of valu
3 min read
Node.js fs.write() Method File-writing is an important aspect of programming. Every programming language has a well-defined file module that can be used to perform file operations. JavaScript and Node.js also have file module which provides various inbuilt methods for performing read, write, rename, delete and other operatio
4 min read
Node.js Stream writable.write() Method The writable.write() method is an inbuilt application programming interface of Stream module which is used to write some data to the Writable stream. The callback function is called once the data has been completely handled. Syntax: writable.write( chunk, encoding, callback) Parameters: This method
2 min read