Skip to content
Related Articles

Related Articles

Node.js http.server.keepAliveTimeout Property
  • Last Updated : 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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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() api
httpServer.listen(PORT, () => { 
    console.log("Server is running at port 3000..."); 
});
  
// Getting keep alive timeout value
// by using keepAliveTimeout api
const v = httpServer.keepAliveTimeout
  
// Display the result
console.log('keep alive time out value :-' + v)

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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() api
var httpServer = http.createServer(
    http2Handlers).listen(3000, () => { 
    console.log("Server is running at port 3000..."); 
}); 
  
// Getting keep alive timeout value
// by using keepAliveTimeout property
const v = httpServer.keepAliveTimeout
  
// Display the result
console.log('keep alive time out value :-' + v)

chevron_right


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

My Personal Notes arrow_drop_up
Recommended Articles
Page :