Javascript Node.js + Express - 无法连接。ERR_CONNECTION_REFUSED

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27890811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:51:17  来源:igfitidea点击:

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

javascriptnode.jsexpress

提问by Tsar Bomba

I followed this basic example:

我遵循了这个基本示例:

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

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 nooutput 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:

文件已生成……它们都在那里。我一步一步地运行它。无论我使用哪种浏览器,我都会收到“无法连接”(Firefox)和“此网页不可用...... ERR_CONNECTION_REFUSED”(Chrome) - 它无法正常工作。我检查了生成的 bin/www 文件,它似乎指示端口 3000。但是,我在生成站点后运行“node app.js”时没有输出。在查看该文件时,我注意到它指向我系统上 Node 的错误路径,因此我将其更改为正确的路径:

#!/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.

没有骰子。没有改变。运行“node app.js”时没有输出,也拉不起来。我知道节点在那里并且正确安装,因为我已经运行了一堆示例代码并稍微玩了一下。

On OS X Yosemite but my firewall is turned off.

在 OS X Yosemite 上,但我的防火墙已关闭。

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

这是怎么回事?令人惊讶的是,在搜索此内容时也发现的信息很少 - 让我犹豫是否要使用 Node.js 构建任何严肃的东西。

回答by Seth

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 npmto run the initial app commands, as you should. The scripts object in package.jsonis extremely handy for abstracting commands.

您的问题是您所遵循的教程很旧。随着时间的推移,Express 生成器的结构发生了巨大的变化。它现在npm用于运行初始应用程序命令,正如您应该的那样。中的脚本对象package.json对于抽象命令非常方便。

Simply cdinto your example app and run:

只需cd进入您的示例应用程序并运行:

npm start

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 docsabove 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.

最后“我注意到它指向我系统上 Node 的错误路径,所以我将其更改为正确的路径”。你应该把它改回来,否则它可能会失败。

回答by Rupali Pemare

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

此外,如果想要保持服务器运行,则使用 nodemon

nodemon bin/www

Which will give same the o/p :

这将给出相同的 o/p :

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