Frage

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.

War es hilfreich?

Lösung

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.

Andere Tipps

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.

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

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top