【发布时间】:2016-11-04 23:42:25
【问题描述】:
我有以下用 Node/Koa 编写的代码,服务于port 80:
const
koa = require('koa'),
route = require('koa-route'),
network = koa(),
common = require('koa-common'),
PORT = 80;
// enable logger middleware
network.use(common.logger('dev'));
// enable static middleware
network.use(common.static(__dirname + '/public'));
network.use(route.get('/', index));
network.use(route.get('/about', about));
function *index() {
this.body = "<h1>Is this message on my computer, or on yours...?</h1>";
}
function *about() {
this.body = "<h2>How about now...</h2>";
}
var server = network.listen(PORT, function () {
console.log('Listening on port %d', server.address().port);
});
我为主机保留了一个地址 (168.192.1.91),在port 80 上设置了到此地址的端口转发,当通过任何协议连接时,在 Windows 10 防火墙中为port 80 设置了一个例外,并且用You Get Signal测试:
确认端口当前处于打开状态。当我浏览到localhost:80 时,我可以看到默认页面。但是,当我在浏览器中输入计算机的公共 IP 地址时(我输入的是我部分模糊的地址,我认为应该是正确的):
此页面无法加载并出现以下错误:
This site can’t be reached
109.[...] took too long to respond.
Try:
Reloading the page
Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_TIMED_OUT
并且 Koa 日志中没有任何活动(当我通过 localhost:80 浏览到那里时,它记录正常)。任何想法可能会阻止连接?
我也试过像这样添加主机地址作为第二个参数:
const HOST = "127.0.0.1";
var server = network.listen(PORT, HOST, function () {
console.log('Listening on port %d', server.address().port);
});
这适用于环回地址,但是当我指定我的公共 IP 时出现此错误:
C:\Sites\order-server>node --harmony cheese.js
events.js:154
throw er; // Unhandled 'error' event
^
Error: listen EADDRNOTAVAIL [My public IP here]:80
at Object.exports._errnoException (util.js:890:11)
at exports._exceptionWithHostPort (util.js:913:20)
at Server._listen2 (net.js:1221:19)
at listen (net.js:1270:10)
at net.js:1379:9
at _combinedTickCallback (node.js:386:13)
at process._tickCallback (node.js:407:11)
at Function.Module.runMain (module.js:449:11)
at startup (node.js:142:18)
at node.js:939:3
【问题讨论】:
-
你的路由器的端口也打开了吗?
-
@BubbleHacker 我设置了端口转发。这不是一回事吗?
标签: node.js server webserver portforwarding koa