好吧,这真的很奇怪,我在实现一个简单的服务器时得到了EADDRINUSE,
我换了港口
查找已在运行的节点js进程
尝试使用killall-9节点
通过sudo netstat-alpn | grep节点查找囤积端口的进程
var http= require('http');
var server=http.createServer(function(request,response){
response.end("kameha kameha....");
}).listen("162.x.x.38",8010);
process.stdin.resume();
process.on('uncaughtException',function(err){
console.log("closing due to uncaught exception:"+err.message+" "+err.data);
server.close();
process.exit(1);
});
process.on('exit',function(){
console.log("exiting the program");
server.close();
process.exit(0);
});
process.on('SIGINT', function(){
console.log("caught cntrl +c closing the program and performing cleanups");
server.close();
process.exit(0);
});
仍然没有帮助,我在一个由bluehost托管的vps上运行centos 6.7,所以让我总结一下,没有节点进程在运行,没有prcocess在囤积端口
奇怪的是,当我将主机设置为本地主机时,它开始工作,但仍然无法通过wget连接它
wget本地主机:8010
--2015-05-22 15:19:15--http://localhost:8010/
正在解析本地主机。。。●1127.0.0.1美元
正在连接到本地主机|:::1 |:8010。。。失败:连接被拒绝。
正在连接到本地主机| 127.0.0.1 |:8010。。。失败:连接被拒绝。
当作为本地主机运行时,netstat提供以下信息:
sudo netstat-alp | grep节点
unix 2[ACC]流侦听453187 23439/节点本地主机
netstat命令声明的协议是unix而不是tcp,虽然我对linux没有太多的经验,所以….但是很奇怪,因为上面的代码在windows中运行良好,但是我不知道这里发生了什么..请帮助!!
最佳答案
http.listen()的参数是向后的。
从node.js文档:
server.listen(端口[,主机名][,待办事项][,回调])#开始
接受指定端口和主机名上的连接。如果
主机名被省略,服务器将接受指向
任何IPv4地址(INADDR_any)。
来自上面docs链接的正确调用如下:
简单,可以在主机的任何地址上工作
var server=http.createServer(function(request,response){
response.end("kameha kameha....");
}).listen(8010);
仅绑定到本地主机
var server=http.createServer(function(request,response){
response.end("kameha kameha....");
}).listen(8010, "127.0.0.1");
绑定到1.2.3.4类似
var server=http.createServer(function(request,response){
response.end("kameha kameha....");
}).listen(8010, "1.2.3.4");