Node.js http.ClientRequest.abort() Method
The http.ClientRequest.abort() is an inbuilt application programming interface of class Client Request within http module which is used to abort the client request.
Syntax:
ClientRequest.abort()
Parameters: This method does not accept any argument as a parameter.
Return Value: This method does not return any value
Example 1: Filename: index.js
Javascript
// Node.js program to demonstrate the // request.abort() APi // Importing http module const http = require('http'); // Create an HTTP server const server = http.createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('okay'); }); // Now that server is running server.listen(3000, '127.0.0.1', () => { console.log("Server is started") // Making a request const options = { port: 3000, host: '127.0.0.1', headers: { 'Connection': 'Upgrade', 'Upgrade': 'websocket' } }; // Getting client request const req = http.request(options); // Aborting the request // by using abort() method req.abort() // Emit the message req.on('abort',()=>{ console.log("client request is aborted") }) }); |
chevron_right
filter_none
Run index.js file using below command:
node index.js
Output:
client request is aborted
Example 2: Filename: index.js
Javascript
// Node.js program to demonstrate the // request.abort() APi // Importing http module const http = require('http'); // Creating an HTTP server http.createServer((req, res) => {}) .listen(3000, '127.0.0.1', () => { // Getting clinet request const req = http.request({ port: 3000, host: '127.0.0.1', }); // Aborting the request // by using abort() api req.abort() // Emit the message req.on('abort',()=>{ console.log("client request is aborted") process.exit(0); }) }); |
chevron_right
filter_none
Run index.js file using below command:
node index.js
Output:
client request is aborted
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http.html#http_request_abort

