Node.js response.writeContinue() Method
Last Updated :
23 Sep, 2020
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() is called internally even when no listener is attached.
In order to get a response and a proper result, we need to import ‘http’ module.
Import:
const http = require('http');
Syntax:
response.writeContinue();
Parameters: This method does not accept any parameter.
Return Value: It does not return any value, instead sends an HTTP/1.1 100 Continue message to the client, indicating that the request body should be sent.
The below example illustrates the use of the response.writeContinue() method in Node.js.
Example 1: Program without response.writeContinue() method.
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 Data..." );
});
const buf = Buffer.alloc(11, 'aGVsbG8gd29ybGQ=' , 'base64' );
response.write(buf, 'utf8' , () => {
console.log( "Writing Buffer 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 Data…
Writing Buffer Data…
Now run http://localhost:3000/ in the browser.
Output: In-Browser
Heyy geeksforgeeks hello world ok
Example 2: Using response.writeContinue() method.
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 Data..." );
});
response.writeContinue();
const buf = Buffer.alloc(11, 'aGVsbG8gd29ybGQ=' , 'base64' );
response.write(buf, '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 Data…
Writing Buffer Data…
Reference: https://nodejs.org/api/http.html#http_response_writecontinue
Share your thoughts in the comments
Please Login to comment...