Express Node.js doesn't work

13,684

Solution 1

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.

Solution 2

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.

Share:
13,684
Abhishek Mhatre
Author by

Abhishek Mhatre

Updated on September 15, 2022

Comments

  • Abhishek Mhatre
    Abhishek Mhatre almost 2 years

    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.

  • conor909
    conor909 about 8 years
    I'm getting the correct log in the console, but my browser is hanging when I try to visit localhost:3000 - any ideas?
  • Zeyad Shaban
    Zeyad Shaban over 3 years
    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'))