Skip to content
Related Articles

Related Articles

Node.js http.server.setTimeout() Method
  • Last Updated : 19 Jan, 2021

The http.server.setTimeout() is an inbuilt application programming interface of class Server within HTTP module which is used to set the time out value for the socket.

Syntax:

server.setTimeout([msecs][, callback])

Parameters: This method takes the socket time out value in a millisecond.

Return Value: This method returns nothing but a call back function for further operation.

Example 1: Filename: index.js



Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the  
// server.setTimeout() APi
    
// 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..."); 
  }); 
}); 
    
// Listening to http Server 
// by using listen() api
httpServer.listen(PORT, () => { 
    console.log(
    "Server is running at port 3000..."); 
});
  
httpServer.setTimeout(3000,()=>{
  
  console.log(
    "Socket is destroyed due to timeout")
  
    // Closing server 
    // by using close() api
    httpServer.close(()=>{
        console.log("Server is closed")
    })
})

chevron_right


Run index.js file using below command:

node index.js

Console output:

Server is running at port 3000...
Displaying the result...
Displaying the result...
Socket is destroyed due to timeout
Socket is destroyed due to timeout
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.setTimeout() APi
    
// 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..."); 
  });
  }; 
    
// Listening to http Server 
// by using listen() api
var httpServer = http.createServer(
    http2Handlers).listen(3000, () => { 
    console.log("Server is running at port 3000..."); 
}); 
  
httpServer.setTimeout(3000,()=>{
    console.log(
    "Socket is destroyed due to timeout")
    
      // Closing server 
      // by using close() api
      httpServer.close(()=>{
          console.log("Server is closed")
      })
    })

chevron_right


Run index.js file using below command:

node index.js

Console output:

Server is running at port 3000...
Displaying the result...
Displaying the result...
Socket is destroyed due to timeout
Socket is destroyed due to timeout
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_settimeout_msecs_callback

My Personal Notes arrow_drop_up
Recommended Articles
Page :