8

I installed express 4 along with node.js,npm and express-generator on my ubuntu 12.04 and created an app using the following commands:

express test --hogan -c less
cd test && npm install
node app.js

Now what I should get is "Express server running on port 3000" but instead the command simply executes and doesn't leave any message or error. So I have no idea of which port express is running on or whether or not it is running at all. So does anyone know what's going wrong in it? Thanks in advance.

4 Answers 4

17

The new express-generator does not add the app.listen statement in the auto-generated app.js file. It could be a bug? What you can do is add a statement like

app.listen(3000, function () {
  console.log("express has started on port 3000");
});

This will instruct express to listen on port 3000 and print out a helpful message on the console as well.

2
  • I'm getting the correct log in the console, but my browser is hanging when I try to visit localhost:3000 - any ideas?
    – conor909
    Apr 14, 2016 at 18:02
  • 1
    Either because you don't have a route handler or because you didn't return any response. app.get('/any_url', (req, res) => res.send('something to end the infinite event loop connection')) Jan 2, 2021 at 18:56
8

Auto generated express apps are not supposed to be started by running node app.js.

If you look in your package.json file, you should see something like

"scripts": {
    "start": "node ./bin/www"
}

This is how the auto-generated app is designed to be started. You can start it either by running exactly what it says there (i.e. node ./bin/www" from the root directory of your project), or the more proper way is to start it using npm start from the root directory.

0

I was having this problem locally. I found the line:

var port = process.env.PORT || 3000;

or

var port = 
process.env.PORT ?
process.env.PORT : 3000;

Did not work for me.

Just gives me "ERR_CONNECTION_REFUSED". But no errors reported from nodemon.

Solution: Be a bit more robust with the logic:

var DEFAULT_PORT = 3000;
var port         = DEFAULT_PORT;
var maybe_port = process.env.PORT;
if( typeof maybe_port === "number" ){
    port = maybe_port;
}

Then, don't forget:

app.listen( port );  
0

For noobs: check if the port number specified in your application the same as the post number you have typed in your browser's searchbar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.