Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
668 views
in Technique[技术] by (71.8m points)

javascript - Error: listen EACCES 0.0.0.0:80 OSx Node.js

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

 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you need to run the server on port 80 you should use a reverse proxy like nginx that 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).

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.

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

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";
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...