简体   繁体   中英

How to fix node.js error when trying to execute?

I am starting to work with server side (node.js), and am having some errors when I try to execute it. I am using 127.0.0.1:80 as the server, but I am experiencing some errors when I try to run it.

Console:

Server running at http://127.0.0.1:80/
node:events:505
      throw er; // Unhandled 'error' event
      ^

Error: Listen EACCES: permission denied 127.0.0.1:80
  //at Server.setupListenHandle [as _listen2] (node:net:1363:21)
  at listenInCluster (node:net:1428:12)
  at doListen (node:net:1567:7)
  at processTicksAndRejections (node:internal/process/task_queues:83:21)//
Emitted 'error' event on Server instance at:
  at emitErrorNT (node:net:1407:8)
  at processTicksAndRejections (node:internal/process/task_queues:83:21) {
       code: 'EACCES',
       errno: -13,
       syscall: 'listen',
       address: '127.0.0.1',
       port: 80
 }

Javascript:

 var http = require('http');
 http.createServer(function (req, res) {
   res.writeHead(200, {'Content-Type': 'text/plain'});
   res.end('Hello World\n');
 }).listen(80, '127.0.0.1');
 console.log('Server running at http://127.0.0.1:80/');

package.json:

     {
   "name": "test-web-server",
   "version": "1.0.0",
   "description": "Node.js test server. Use for whatever bs you want to do",
   "main": "index.js",
   "scripts": {
     "start": "node server.js",
     "test": "echo \"Error: no test specified\" && exit 1"
   },
   "author": "PlazL",
   "license": "ISC"
 }

a rule of thumb is that listening to ports less than 1024 often requires elevated privileges. Try changing the port to 8080, or elevate your privileges with sudo.

For brevity here is the change in your code:

 var http = require('http');
 http.createServer(function (req, res) {
   res.writeHead(200, {'Content-Type': 'text/plain'});
   res.end('Hello World\n');
 }).listen(8080, '127.0.0.1'); // change port here (80 -> 8080)
 console.log('Server running at http://127.0.0.1:80/');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM