[英]Run node.js on a web server?
我正在尝试在加载网页时运行nodejs应用程序,但是它不起作用。 我尝试使用express在节点内创建服务器,但是每当尝试加载网页时,都会出现错误:ERR_CONNECTION_TIMED_OUT。
这是我用来连接服务器的代码:
var app = require('express')();
app.get('/test', function (req, res) {
console.log('web page opened');
});
app.listen(3000, function () {
console.log('Listening on port 3000');
});
在控制台中,我看到“正在侦听端口3000”,但无法连接到网站( http://45.55.71.134:3000/test )。
有人知道对此有任何解决方案吗? 谢谢。
get语句表明您正在端口3000侦听“ / test”,但您使用的url为“ / some”。 尝试更改为“ /测试”?
/test
,但正在请求/some
。 改变它。 localhost:3000
连接。 如果成功,则说明您的路由器或防火墙设置存在问题,您可能需要打开该端口。 您的连接正在超时,因为您没有关闭请求。 浏览器仍在等待从服务器接收一些数据,但是您只有console.log("web page opened")
,它位于服务器上而不是浏览器上。 在等待服务器响应后,浏览器放弃了,这就是为什么您会收到该错误的原因。
//Do this
app.get('/test', function (req, res) {
//instead of this
console.log('web page opened'); //This doesn't do anything.
//Try this instead
res.send("web page opened");
});
您需要始终关闭请求。 您也可以像这样调用next()
函数来强制关闭-
app.get('/test', function (req, res, next) {
console.log("web somehting");
next();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.