简体   繁体   English

无法运行简单的node.js应用抛出错误:监听EACCES

[英]Cant run simple node.js app throwing Error: listen EACCES

I ussually code on c9 I am trying to get work on my local environment, Im a trying a ver silly app but I get an error. 我通常在c9上编写代码,试图在我的本地环境上工作,我正在尝试一个非常愚蠢的应用程序,但出现错误。

APP: APP:

//APP IMPORTS
var express    = require('express'),
    app        = express()


//INDEX PAGE
app.get('/', function(req, res) {
   res.send('Welcome');
});


app.listen('localhost', 30000, function() {
   console.log("mirror server started!"); 
});

Error: 错误:

λ node app.js
events.js:167
      throw er; // Unhandled 'error' event
      ^

Error: listen EACCES localhost
    at Server.setupListenHandle [as _listen2] (net.js:1269:19)
    at listenInCluster (net.js:1334:12)
    at Server.listen (net.js:1432:5)
    at Function.listen (c:\Moi\mirror\node_modules\express\lib\application.js:618:24)
    at Object.<anonymous> (c:\Moi\mirror\app.js:12:5)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
Emitted 'error' event at:
    at emitErrorNT (net.js:1313:8)
    at process._tickCallback (internal/process/next_tick.js:63:19)
    at Function.Module.runMain (internal/modules/cjs/loader.js:744:11)
    at startup (internal/bootstrap/node.js:285:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)

Not sure why this is happening I was trying first with process.env.IP and process.env.PORT but I didnt want to set the environment vars yet, and I dont know what ip node uses for default, so I tried like thisnot sure why it doesnt work, I also tried with an ip like '127.0.0.22' and PORT 3000, etc. 不知道为什么会这样,我首先尝试使用process.env.IP和process.env.PORT,但是我还不想设置环境变量,而且我不知道默认情况下ip节点使用什么,所以我尝试这样确定为什么它不起作用,我也尝试使用“ 127.0.0.22”和PORT 3000等IP。

You have the port number and "bind host" in the wrong order. 您具有错误的端口号和“绑定主机”。 It should be: 它应该是:

app.listen(30000, 'localhost', ...)

Try following to get your app started: 尝试以下操作以启动您的应用:

//APP IMPORTS
var express    = require('express'),
    app        = express()


//INDEX PAGE
app.get('/', function(req, res) {
   res.send('Welcome');
});

app.set('port', 30000);

app.listen(app.get('port'), function() {
   console.log("mirror server started!"); 
});

Your server should automaticaly get localhost as host and change as soon as you work on production with your server host. 您的服务器应自动将localhost作为主机,并在使用服务器主机进行生产时立即进行更改。

Another way 其他方式

Just replace the order of your listen function parameter like: 只需替换您的listen函数参数的顺序即可,例如:

app.listen(30000, 'localhost',  function() {
   console.log("mirror server started!"); 
});

The signature like express described looks like this: app.listen([port[, host[, backlog]]][, callback]) 像express这样的签名描述如下: app.listen([port[, host[, backlog]]][, callback])

Source: Express documentation 来源: Express文档
You can find here a small explanation on how express handelt server config 您可以在此处找到有关如何快速处理handelt server config简短说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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