Javascript 错误:听 EACCES 0.0.0.0:80 OSx Node.js

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35068712/
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 17:14:31  来源:igfitidea点击:

Error: listen EACCES 0.0.0.0:80 OSx Node.js

javascriptnode.jsmacos

提问by olivier

I'm following a tutorial in a angularJS book and have to setup a server. This is the server.js file:

我正在学习 angularJS 书中的教程,并且必须设置服务器。这是 server.js 文件:

 var express = require('express');
  var app = express();
   app.use('/', express.static('./'));
    app.listen(80);

I get this error:

我收到此错误:

$ node server.js
events.js:154
      throw er; // Unhandled 'error' event
      ^

Error: listen EACCES 0.0.0.0:80

I know already, that the Error EACCES means that i don't have access rights to the port 80, but i don't know how to fix this. Any help much appreciated!

我已经知道,错误 EACCES 意味着我没有访问端口 80 的权限,但我不知道如何解决这个问题。非常感谢任何帮助!

采纳答案by vesse

If you need to run the server on port 80 you should use a reverse proxy like nginxthat will run using a system account on a privileged port and proxy the requests to your Node.js server running on an unprivileged port (> 1024).

如果您需要在端口 80 上运行服务器,您应该使用像nginx这样的反向代理,它将使用系统帐户在特权端口上运行,并将请求代理到在非特权端口(> 1024)上运行的 Node.js 服务器。

When running in development environment you're pretty much free to run as root (ie. sudo node server.js), but that is rather dangerous in production environment.

在开发环境中运行时,您可以以 root 身份运行(即sudo node server.js),但这在生产环境中相当危险。

Here's a sample nginx config that will see if the request is for a file that exists in the filesystem, and if not, proxy the request to your Node.js server running on port 9000

这是一个示例 nginx 配置,它将查看请求是否针对文件系统中存在的文件,如果不是,则将请求代理到在端口 9000 上运行的 Node.js 服务器

upstream yournodeapp {
  server localhost:9000 fail_timeout=0;
  keepalive 60;
}

server {
  server_name localhost;
  listen 80 default_server;

  # Serve static assets from this folder
  root /home/user/project/public;

  location / {
    try_files $uri @yournodeapp;
  }

  location @yournodeapp {
    proxy_pass http://yournodeapp;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }
}

回答by DeveloperCoding

to give root access to node and start server on port 80 you can do

授予对节点的 root 访问权限并在端口 80 上启动服务器,您可以这样做

sudo node app.js

this will start the server giving permission on port 80

这将启动服务器授予端口 80 的权限

回答by azatar

Foremost, do not run as root.That's asking for 'it'. "It" is really bad. Go see the movie. Then, don't run your node web project as root.

首先,不要以 root 身份运行。那就是要求'它'。“它”真的很糟糕。去看电影吧。然后,不要以 root 身份运行您的节点 Web 项目。

// DO NOT DO THIS!
$ sudo node app.js

// DO NOT DO THIS EITHER!
$ sudo su -
# node app.js

Instead, use PM2and authbindto do this:

相反,使用PM2authbind来执行此操作:

// %user% is whatever user is running your code
sudo touch /etc/authbind/byport/80
sudo chown %user% /etc/authbind/byport/80
sudo chmod 755 /etc/authbind/byport/80

Next, add this alias to your ~/.bash_aliasesor ~/.bashrcor ~/.bash_profile:

接下来,将此别名添加到您的~/.bash_aliasesor~/.bashrc~/.bash_profile

alias pm2='authbind --deep pm2'

Then, try it with pm2:

然后,用 pm2 试试:

pm2 start app.js

回答by Josh P

On Windows I fixed this by setting Express to listen on port 8080 for HTTP and 8443 for HTTPS. It really doesn't like using those lower number ports. Also I have IIS installed and running so it might be some sort of port conflict there too.

在 Windows 上,我通过将 Express 设置为在端口 8080 上侦听 HTTP 和 8443 上侦听 HTTPS 来解决此问题。它真的不喜欢使用那些较低数量的端口。此外,我安装并运行了 IIS,因此那里也可能存在某种端口冲突。