简体   繁体   中英

Creating a simple node.js static server

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

The error msg I receive in the cli via "node server.js" is: events.js:160 throw er; // Unhandled 'error' event ^

Error: listen EACCES 0.0.0.0:80 at Object.exports._errnoException (util.js:1018:11) at exports._exceptionWithHostPort (util.js:1041:20) at Server._listen2 (net.js:1245:19) at listen (net.js:1294:10) at Server.listen (net.js:1390:5) at EventEmitter.listen (G:\\angular\\node_modules\\express\\lib\\application.js:618:24) at Object. (G:\\angular\\server.js:4:5) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32)

Any ideas why a simple bit of code would generate errors? I got the code from an older angularjs book I'm trying to learn from. Changes with node or express possibly?

On Unix, all ports below 1024 are so called Privileged Ports . Only root or other specific system users can start services here.

When you're programming with a regular user (as you should), it's customary to start your dev server on ports above 1024. For web servers it's common to use 8080 or 3000 .

The error message Error: listen EACCES 0.0.0.0:80 also gave you a hint. EACCESS means that you do not have the rights to open a server on port 80. Only the root user does for running production code.

Also one piece of advice: AngularJS has changed a lot in the last years. So if you want to learn it, don't use an older book. Much of what you'd learn is probably obsolete and done differently now.

Error: listen EACCES means you don't have permission to listen on that port. Try different port.

And static content should be served like this

app.use(express.static(__dirname + '/'));

__dirname represent current directory.

This code is working for me, please try it:

var express = require('express');

var app = express.createServer();

app.get('/', express.static(__dirname + 'your path'));

app.listen(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