简体   繁体   中英

Node.js + Express - Can't connect. ERR_CONNECTION_REFUSED

I followed this basic example:

http://shapeshed.com/creating-a-basic-site-with-node-and-express/

Files were generated...they're all there. I ran it all step-by-step. No matter which browser I use, I get "Unable to connect" (Firefox) and "This webpage is not available ... ERR_CONNECTION_REFUSED" (Chrome) - it's just not working. I checked the generated bin/www file and it seems to indicate port 3000. However, I got no output when I ran "node app.js" after generating the site. Upon looking at that file, I noticed it pointed to the wrong path for Node on my system, so I changed it to the correct one:

#!/usr/local/bin/ node

/**
 * Module dependencies.
 */

var app = require('../app');
var debug = require('debug')('rwc:server');
var http = require('http');

/**
 * Get port from environment and store in Express.
 */

var port = parseInt(process.env.PORT, 10) || 3000;
app.set('port', port);

/**
 * Create HTTP server.
 */

var server = http.createServer(app);

/**
 * Listen on provided port, on all network interfaces.
 */

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
 * Event listener for HTTP server "error" event.
 */

function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;
  }

  // handle specific listen errors with friendly messages
  switch (error.code) {
    case 'EACCES':
      console.error('Port ' + port + ' requires elevated privileges');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error('Port ' + port + ' is already in use');
      process.exit(1);
      break;
    default:
      throw error;
  }
}

/**
 * Event listener for HTTP server "listening" event.
 */

function onListening() {
  debug('Listening on port ' + server.address().port);
}

No dice. Nothing changed. No output when running "node app.js" and can't pull it up. I know node is there and correctly installed since I've already run a bunch of example code and played with it a bit.

On OS X Yosemite but my firewall is turned off.

What's going on? Surprisingly little info found when search on this too - makes me hesitant to build anything serious with Node.

Your problem is that the tutorial you're following is very old. Express generator has changed it's structure immensely over time. It now utilizes npm to run the initial app commands, as you should. The scripts object in package.json is extremely handy for abstracting commands.

Simply cd into your example app and run:

npm start

You'll see the following in your terminal:

$ npm start

> example@0.0.0 start /Users/your-user/example
> node ./bin/www

and enjoy!

The rest of that tutorial aside from setting it up is still pretty accurate though. I'd consult the docs above anything to be honest. Just my opinion though.

Lastly "I noticed it pointed to the wrong path for Node on my system, so I changed it to the correct one". You should change that back or it might fail.

Also if one wants to keep the server running , then use nodemon

nodemon bin/www

Which will give same the o/p :

[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node ./bin/www bin/wwww`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM