Node.js http.server.keepAliveTimeout Property - GeeksforGeeks

20 Jan, 2021
The http.server.keepAliveTimeout is an inbuilt application programming interface of class Server within http module which is used to get the number of milliseconds of inactivity a server needs to wait for additional incoming data.
Syntax:
server.keepAliveTimeout
Parameters: This Api does not accept any argument as a parameter.
Return Value: This Method returns the number of milliseconds of inactivity a server needs to wait for additional incoming data.
Example 1: Filename: index.js
Javascript
// Node.js program to demonstrate the  // server.keepAliveTimeout property    // 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){     // Getting the reference of the  // underlying socket object  // by using socket API  const value = response.socket;      // Display result  // by using end() api  response.end( "Socket buffersize : "       + value.bufferSize, 'utf8', () => {       console.log("displaying the result...");         // Closing server       // by using close() api      httpServer.close(()=>{          console.log("server is closed")      })  }); });     // Listening to http Server // by using listen() apihttpServer.listen(PORT, () => {     console.log("Server is running at port 3000..."); });  // Getting keep alive timeout value// by using keepAliveTimeout apiconst v = httpServer.keepAliveTimeout  // Display the resultconsole.log('keep alive time out value :-' + v)
Run index.js file using below command:
node index.js
Console Output:
keep alive time out value :-5000
Server is running at port 3000...
displaying the result...
displaying the result...
server is closed
server is closed
Browser Output: Paste the localhost address http://localhost:3000/ in the search bar of the browser.
Socket buffersize : 0
Example 2: Filename: index.js
Javascript
// Node.js program to demonstrate the  // server.keepAliveTimeout property    // Importing http module var http = require('http');   // Request and response handler const http2Handlers = (request, response) => {       // Getting the reference of the  // underlying socket object  // by using socket API  const value = response.socket;      // Display result  // by using end() api  response.end( "Socket local address : "       + value.localAddress, 'utf8', () => {       console.log("displaying the result...");         // Closing server       // by using close() api      httpServer.close(()=>{          console.log("server is closed")      })  });};     // Listening to http Server // by using listen() apivar httpServer = http.createServer(    http2Handlers).listen(3000, () => {     console.log("Server is running at port 3000..."); });   // Getting keep alive timeout value// by using keepAliveTimeout propertyconst v = httpServer.keepAliveTimeout  // Display the resultconsole.log('keep alive time out value :-' + v)
Run index.js file using below command:
node index.js
Console Output:
keep alive time out value :-5000
Server is running at port 3000...
displaying the result...
displaying the result...
server is closed
server is closed
Browser Output: Paste the localhost address http://localhost:3000/ in the search bar of the browser.
Socket local address : ::1
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http.html#http_server_keepalivetimeout
Node.js-Methods
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Express.js express.Router() Function
JWT Authentication with Node.js
Express.js req.params Property
Difference between npm i and npm ci in Node.js
Mongoose Populate() Method
Roadmap to Become a Web Developer in 2022
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Convert a string to an integer in JavaScript