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);


Definition and Usage

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


Syntax

server.listen(porthostnamebacklogcallback);

Parameter Values

ParameterDescription
portOptional. Specifies the port we want to listen to
hostnameOptional. Specifies the IP address we want to listen to
backlogOptional. Specifies the max length of the queue of pending connections. Default 511
callbackOptional. Specifies a function to be executed when the listener has been added

Technical Details

Return Value:None
Node.js Version:0.1.90


ref : https://www.w3schools.com/nodejs/met_server_listen.asp

+ Recent posts