THE WORLD'S LARGEST WEB DEVELOPER SITE

Node.js server.listen() Method

❮ HTTP Server


Example

Create a server that listens on port 8080 of your computer.

When port 8080 get accessed, write "Hello World!" back as a response:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.write('Hello World!');
  res.end();
}).listen(8080);
Run example »

Definition and Usage

The server.listen() method creates a listener on the specified port or path.


Syntax

server.listen(port, hostname, backlog, callback);

Parameter Values

Parameter Description
port Optional. Specifies the port we want to listen to
hostname Optional. Specifies the IP address we want to listen to
backlog Optional. Specifies the max length of the queue of pending connections. Default 511
callback Optional. Specifies a function to be executed when the listener has been added

Technical Details

Return Value: None
Node.js Version: 0.1.90

❮ HTTP Server