'Express.js showing ERR_CONNECTION_REFUSED

I have installed node(v4.1.2) and express(4.13.3)

Node Server code:

var express = require('express');
var app = express();

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

var server = app.listen(3000, function () {
  var host = server.address().address;
  var port = server.address().port;

  console.log('Example app listening at http://%s:%s', host, port);
});

After running the node file and upon calling http://localhost:3000/ gives me ERR_CONNECTION_REFUSED.



Solution 1:[1]

You are listening on port 3000.

So Try http://localhost:3000

Solution 2:[2]

Have you run in powershell at server directory?

node app.js

Solution 3:[3]

At first install Node.js body parsing middleware.

Installation: npm install body-parser

Then add the following lines:

var bodyParser = require('body-parser');
app.use(bodyParser.json());

I think your problem will be solved. For more details, visit - http://expressjs.com/en/resources/middleware/body-parser.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hiren S.
Solution 2 Pan Markosian
Solution 3