【问题标题】:Restify won't listen on ipv6Restify 不会在 ipv6 上监听
【发布时间】:2014-09-10 23:49:07
【问题描述】:

使用来自 restify 的 README 的示例服务器,它启动并仅绑定到 ipv4 地址上的端口 8080。有没有办法让它绑定到所有地址? Mac OS X。

$ node test.js
myapp listening at http://0.0.0.0:8080

$ netstat -an | grep 8080
tcp4       0      0  *.8080                 *.*                    LISTEN

这有点烦人,因为'localhost'首先尝试v6地址,而一些客户端(例如:curl)不尝试其他地址。我必须记住始终明确输入 127.0.0.1。

$ curl http://localhost:8080/
curl: (7) Failed to connect to localhost port 8080: Connection refused

$ curl http://127.0.0.1:8080/
{"code":"ResourceNotFound","message":"/ does not exist"}

【问题讨论】:

    标签: restify


    【解决方案1】:

    假设消息 myapp listening at http://0.0.0.0:8080 来自您的 test.js 中的类似内容

    app.listen(8080, function(err) {
      console.log("myapp listening at %s", app.url);
    });
    

    改用这个:

    app.listen(8080, '::', function(err) {
      console.log("myapp listening at %s", app.url);
    });
    

    这将使 node.js 监听 IPv4 和 IPv6 接口。使用模块 express 和 restify 测试正常。

    【讨论】:

    • 太棒了,谢谢!它应该在哪些节点版本上工作?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-27
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-20
    相关资源
    最近更新 更多