Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Node.js http.server.listen() Method

  • Last Updated : 12 Jan, 2021

The http.server.listen() is an inbuilt application programming interface of class Server within the http module which is used to start the server from accepting new connections.

Syntax:

const server.listen(options[, callback])

Parameters: This method accepts the following two parameters:

  • option: It can be the port, host, path, backlog, exclusive, readableAll, writableAll, ipv6Only, etc depending upon user need.
  • callback: It is an optional parameter, it is the callback function that is passed as a parameter.

Return Value: This method returns nothing but a callback function.

Example 1: Filename-index.js



Javascript




// Node.js program to demonstrate the  
// server.listen() method
  
// 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 method
    const value = response.socket;
  
    // Display result
    // by using end() method
    response.end("socket buffersize : " 
        + value.bufferSize, 'utf8', () => {
            console.log("displaying the result...");
  
            // Closing server 
            // by using close() method
            httpServer.close(() => {
                console.log("server is closed")
            })
        });
});
  
// Listening to http Server 
// by using listen() method
httpServer.listen(PORT, () => {
    console.log("Server is running at port 3000...");
});

Run the index.js file using the following command:

node index.js

Output:

Server is running at port 3000...
displaying the result...
server is closed

Now run http://localhost:3000/ in the browser and you will see the following output on screen:

socket buffersize : 0

Example 2: Filename-index.js

Javascript




// Node.js program to demonstrate the  
// server.listen() method
  
// Importing http module 
var http = require('http');
  
// Request and response handler 
const httpHandlers = (request, response) => {
  
    // Getting the reference of the
    // underlying socket object
    // by using socket method
    const value = response.socket;
  
    // Display result by using end() method
    response.end("socket local address : " 
        + value.localAddress, 'utf8', () => {
            console.log("displaying the result...");
  
            // Closing server by using close() method
            httpServer.close(() => {
                console.log("server is closed")
            })
        });
};
  
// Listening to http Server 
// by using listen() method
var httpServer = http.createServer(
    httpHandlers).listen(3000, () => {
    console.log("Server is running at port 3000...");
});

Run the index.js file using the following command:

node index.js

Output:

Server is running at port 3000...
displaying the result...
server is closed

Now run http://localhost:3000/ in the browser and you will see the following output on screen:

socket local address : ::1

Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http.html#http_server_listen




My Personal Notes arrow_drop_up
Recommended Articles
Page :