简体   繁体   English

侦听不同的 IP 地址

[英]Listen to a different IP address

This is my code:这是我的代码:

var server = express();

// Create the HTTP server
http
    .createServer(server)
    .listen(80, '10.10.10.10');

Once the server has been started, how can I dynamically change the listened IP address, say to 11.11.11.11 instead of 10.10.10.10 .服务器启动后,如何动态更改监听的 IP 地址,比如11.11.11.11而不是10.10.10.10 Is there an "unlisten" method?有“不听”的方法吗?

you have to use server.close() not app.close()..你必须使用 server.close() 而不是 app.close()。

var express = require('express')
  , http = require('http')
  , app = express()
  , server = http.createServer(app)

app.get('/',function(req,res){
  ...
})

server.listen(8000,'127.0.0.1',function(){
 server.close(function(){
   server.listen(8001,'192.168.0.202')
 })
})

should work应该管用

我认为您正在寻找的“不听”功能称为“关闭”: http : //nodejs.org/api/http.html#http_server_close_callback

What your are trying to accomplish is quite non-standard in my opinion.在我看来,你想要完成的事情是非常不标准的。 I would suggest server.close() .我建议server.close() Close will wait all request to finish and trigger the "close" event.关闭将等待所有请求完成并触发“关闭”事件。 You can bind on that event to listen on the new IP.您可以绑定该事件以侦听新 IP。 This is quite weird tho.这很奇怪。

app.listen(3000,'0.0.0.0',function(){
  console.log('Server running at http://127.0.1.1:8000/')
})

will work with express将与快递一起工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在特定IP地址EADDRNOTAVAIL上的node.js server.listen - node.js server.listen on a specific IP address EADDRNOTAVAIL 如何在侦听时获取Restify服务器的确切IP地址? - How to get the exact IP address of the Restify server on listen? 如何使用Node.js监听我的公共IP地址端口 - How to listen on my public IP address port using Nodejs 节点哪个端口/ IP地址要监听azure ubuntu vm - node which port / ip address to listen to for azure ubuntu vm node-request ip addres与远程ip地址不同 - node-request ip addres different from remote ip address 为什么req.socket.address()。address显示的IP地址与实际的公共IP地址不同? - Why does req.socket.address().address show a different IP address than my actual public IP address? 如何在其他IP地址启动Express 4.x Server? - How to start Express 4.x Server at Different IP Address? 节点 static 服务于不同的索引。html 用于某个 IP 地址 - Node static serve different index.html for a certain IP address Node JS将请求绑定到其他接口或IP地址 - Node js bind request to different interface or IP address 如何在Node.js的同一进程中侦听同一地址上的不同UDP端口 - How to listen to different UDP ports on the same address within the same process in Node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM