最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js + Express - Can't connect. ERR_CONNECTION_REFUSED - Stack Overflow

programmeradmin2浏览0评论

I followed this basic example:

/

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.

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.

Share Improve this question edited Jan 11, 2015 at 19:11 Tsar Bomba asked Jan 11, 2015 at 18:54 Tsar BombaTsar Bomba 1,1066 gold badges31 silver badges63 bronze badges 16
  • 1 I wouldn't be hesitant. 90% of computer error is user error. Node isn't the problem. When you run node app.js, you get no output at all? Also please update the question with the output received when you run which node. – Seth Commented Jan 11, 2015 at 18:57
  • 2 What URL are you accessing? and please give the code that creates the server, will be helpful – surajck Commented Jan 11, 2015 at 19:00
  • 1 Does the process stay running? Are you running it from the command line so you can see it is still running. – Ted Johnson Commented Jan 11, 2015 at 19:01
  • 1 If you get no output at all, then you did something wrong in following the instructions provided. You should at least get a console log statement that the app is listening on whatever port you set it to. – Paul Commented Jan 11, 2015 at 19:04
  • 2 So just to clarify, installed node, express, and express-generator. Ran express ..., it generated a directory. You cd'd into it and ran npm i(install). once all dependencies were installed, you ran node app.js and received no output? Can you paste the command and what it does or doesn't return? We're currently using node.js on enterprise level applications with great success. You just need to use it for the right things and not use it for something it wasn't meant for. – Seth Commented Jan 11, 2015 at 19:26
 |  Show 11 more comments

2 Answers 2

Reset to default 11

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

> [email protected] 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`
发布评论

评论列表(0)

  1. 暂无评论