创建一个简单的node.js静态服务器
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?
在Unix上,所有低于1024的端口都被称为Privileged Ports。只有root或其他特定系统用户可以在这里启动服务。
当您使用普通用户进行编程时(通常应该如此),通常会在1024以上的端口上启动您的开发服务器。对于Web服务器,通常使用8080
或3000
。
错误消息Error: listen EACCES 0.0.0.0:80
也给了你一个提示。 EACCESS
表示您无权在端口80上打开服务器。只有root用户才能运行生产代码。
另外一条建议:AngularJS在过去的几年中改变了很多。所以如果你想学习它,不要使用旧书。你学到的很多东西可能已经过时了,现在做的不同。
此代码为我工作,请尝试:
var express = require('express');
var app = express.createServer();
app.get('/', express.static(__dirname + 'your path'));
app.listen(80);
Error: listen EACCES
意味着你没有权限听听那个港口。尝试不同的端口。
和静态内容,应服这样
app.use(express.static(__dirname + '/'));
__dirname
代表当前目录。
您需要将资源名称添加为静态路由(例如包含前端代码或html等的文件夹),而不是另一条路径:) –