Node.js + Express:应用程序不会开始侦听端口 80 [英] Node.js + Express: app won't start listening on port 80

查看:36
本文介绍了Node.js + Express:应用程序不会开始侦听端口 80的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建并启动了一个这样的应用:

I create and launch an app like this:

express -s -t ejs
npm install express
npm install ejs
node app.js

它可以工作(在端口 3000 上).但是当我将端口更改为 80 时,运行 node app.js 会输出:

and it works (on port 3000). But when I go and change the port to 80, then running node app.js outputs this:

node.js:198
throw e; // process.nextTick error, or 'error' event on first tick
          ^
TypeError: Cannot call method 'getsockname' of null
at HTTPServer.address (net.js:746:23)
at Object.<anonymous> (/var/www/thorous/app.js:35:67)
at Module._compile (module.js:432:26)
at Object..js (module.js:450:10)
at Module.load (module.js:351:31)
at Function._load (module.js:310:12)
at Array.<anonymous> (module.js:470:10)
at EventEmitter._tickCallback (node.js:190:26)

这也适用于我的笔记本电脑,但不适用于打开端口 80 的 Amazon EC2 实例.可以弄清楚是什么问题.有什么提示吗?

This works too on my laptop, but not on my Amazon EC2 instance, where port 80 is open. Can figure out what's wrong. Any tips?

推荐答案

您是否以 root 身份启动您的应用程序?因为较低的端口号需要 root 权限.也许 sudo 节点 app.js 有效?

Are you starting your app as root? Because lower port numbers require root privileges. Maybe a sudo node app.js works?

但是,您不应该使用 root 权限在端口 80 上运行任何 node.js 应用程序!!!永远不要!

BUT, you should NOT run any node.js app on port 80 with root privileges!!! NEVER!

我的建议是在前面运行 nginx 作为在端口上运行的 node.js 应用程序的反向代理,例如3000

My suggestions is to run nginx in front as a reverse proxy to your node.js app running on port e.g. 3000

这篇关于Node.js + Express:应用程序不会开始侦听端口 80的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆