簡體   English   中英

如何在其他IP地址啟動Express 4.x Server?

[英]How to start Express 4.x Server at Different IP Address?

我知道這個問題在一百萬年前就被問過了,但是即使經過長時間的研究,我仍然沒有使它起作用。 我的VPS中有一個Express應用程序,它由express-generator 我已經在bin/www文件中這樣做了:

 var port = normalizePort(process.env.PORT || '80');
 app.set('port', port);
 var server = http.createServer(app);

 server.listen(port, '0.0.0.0');   // set it to listen to all incoming IP
 server.on('error', onError);
 server.on('listening', onListening);

我在onListening()添加了一些調試行,所以我有:

function onListening() {
   var addr = server.address();
   var bind = typeof addr === 'string'
           ? 'pipe ' + addr
           : 'port ' + addr.port;
   debug('Listening on ' + bind);
   console.log(addr)
   console.log('Listening on ' + bind);
}

我執行node ,它打印:

{ address: '0.0.0.0', family: 'IPv4', port: 80 }
Listening on port 80

因此,這意味着我的節點已成功啟動。 但是服務器仍然無法訪問(只是在瀏覽器中加載...)。 我知道這與我的防火牆無關,因為我從Node網站編寫了一個小的hello world程序,它在瀏覽器中顯示hello world。 這是我正在工作的hello world程序:

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

這是我的netstat,節點明顯處於運行狀態,正在監聽端口80

Proto Recv-Q Send-Q Local Address           Foreign Address            
State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      492/sshd        
tcp        0      0 0.0.0.0:25              0.0.0.0:*               LISTEN      596/master      
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      5557/mongod     
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      9747/node       

這是我的iptable

 Chain INPUT (policy ACCEPT 108 packets, 10711 bytes)
 pkts bytes target     prot opt in     out     source               destination         
 843 77582 ACCEPT     tcp  --  any    any     anywhere             anywhere             tcp dpt:ssh
 17  2030 ACCEPT     tcp  --  any    any     anywhere             anywhere             tcp dpt:http

 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

 Chain OUTPUT (policy ACCEPT 1497 packets, 191K bytes)
 pkts bytes target     prot opt in     out     source               destination  

有人可以幫忙嗎? 提前致謝。

好吧,在奮斗了一天之后...還閱讀了@ jfriend00和@robertklep的評論。 事實證明,我的防火牆設置很好,並且那里的數據庫安裝存在問題,這就是為什么它顯示空白屏幕而沒有日志,並且僅影響我的VPS的原因。 感謝您的評論!

暫無
暫無

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

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