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
var http = require( 'http' );
const PORT = process.env.PORT || 3000;
var httpServer = http.createServer( function (request, response){
response.write( "Heyy geeksforgeeks " , 'utf8' , () => {
console.log( "Writing string Data..." );
});
response.end( ' ok' );
});
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
var http = require( 'http' );
const PORT = process.env.PORT || 3000;
var httpServer = http.createServer( function (request, response){
var str = "GeeksForGeeks wishes you a warm welcome..." ;
response.write(str, 'utf16' , () => {
console.log( "Writing string Data..." );
});
const buf = Buffer.alloc(11, 'aGVsbG8gd29ybGQ=' , 'base64' );
response.write(buf, 'utf8' , () => {
console.log( "Writing Buffer Data..." );
});
const buff = Buffer.from( ' hello world' , 'utf8' );
response.write(buff, 'utf8' , () => {
console.log( "Writing Buffer Data..." );
});
response.end( ' ok' );
});
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...