Thursday, 28 February 2019

Express: app.listen([port[, host[, backlog]]][, callback]): Listen application on this port


app.listen([port[, host[, backlog]]][, callback])
Binds and listens for the connection at specific port.

app.listen(3000, () => {console.log("Application started in port 3000")});

index.js
var express = require('express')

var app = express()

app.get('/', function (req, res) {
  res.send('Hello World');
});

app.listen(3000, () => {console.log("Application started in port 3000")});


Run index.js, and open the url ‘http://localhost:3000/’ in browser.



If you omit the port number or assign port to 0, then operating system will assign a randomly unused port.

index.js
var express = require('express')

var app = express()

app.get('/', function (req, res) {
  res.send('Hello World');
});

var listener = app.listen(() => {console.log('Application started on port ' + listener.address().port);});

When I ran first time, I seen below message in console.
Application started on port 5361

When I ran second time, I seen below message in console.
Application started on port 5364



Previous                                                 Next                                                 Home

No comments:

Post a Comment