Listen to a different IP address

て烟熏妆下的殇ゞ 提交于 2019-11-27 03:14:37

问题


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. Is there an "unlisten" method?


回答1:


you have to use server.close() not 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




回答2:


I think the "unlisten" function you're looking for is called "close": http://nodejs.org/api/http.html#http_server_close_callback




回答3:


What your are trying to accomplish is quite non-standard in my opinion. I would suggest 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. This is quite weird tho.




回答4:


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

will work with express



来源:https://stackoverflow.com/questions/15003947/listen-to-a-different-ip-address

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!