Node.js http.server.keepAliveTimeout Property
Last Updated :
06 Apr, 2023
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
const http = require( 'http' );
const PORT = process.env.PORT || 3000;
const httpServer = http.createServer(
function (request, response) {
const value = response.socket;
response.end( "Socket buffersize : "
+ value.bufferSize, 'utf8' , () => {
console.log( "displaying the result..." );
httpServer.close(() => {
console.log( "server is closed" )
})
});
});
httpServer.listen(PORT, () => {
console.log( "Server is running at port 3000..." );
});
const v = httpServer.keepAliveTimeout
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
const http = require( 'http' );
const http2Handlers = (request, response) => {
const value = response.socket;
response.end( "Socket local address : "
+ value.localAddress, 'utf8' , () => {
console.log( "displaying the result..." );
httpServer.close(() => {
console.log( "server is closed" )
})
});
};
let httpServer = http.createServer(
http2Handlers).listen(3000, () => {
console.log( "Server is running at port 3000..." );
});
const v = httpServer.keepAliveTimeout
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
Share your thoughts in the comments
Please Login to comment...