簡體   English   中英

無法運行簡單的node.js應用拋出錯誤:監聽EACCES

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

我通常在c9上編寫代碼,試圖在我的本地環境上工作,我正在嘗試一個非常愚蠢的應用程序,但出現錯誤。

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

錯誤:

λ 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)

不知道為什么會這樣,我首先嘗試使用process.env.IP和process.env.PORT,但是我還不想設置環境變量,而且我不知道默認情況下ip節點使用什么,所以我嘗試這樣確定為什么它不起作用,我也嘗試使用“ 127.0.0.22”和PORT 3000等IP。

您具有錯誤的端口號和“綁定主機”。 它應該是:

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

嘗試以下操作以啟動您的應用:

//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!"); 
});

您的服務器應自動將localhost作為主機,並在使用服務器主機進行生產時立即進行更改。

其他方式

只需替換您的listen函數參數的順序即可,例如:

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

像express這樣的簽名描述如下: app.listen([port[, host[, backlog]]][, callback])

來源: Express文檔
您可以在此處找到有關如何快速處理handelt server config簡短說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM