Node.js http.server.keepAliveTimeout Property

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
const http = require('http');
 
// Setting up PORT
const PORT = process.env.PORT || 3000;
 
// Creating http Server
const 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)


Run the index.js file using the 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
const 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
let 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)


Run the index.js file using the 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



Contact Us