使用http模块获取localhost /同一主机的请求

时间:2015-12-18 04:14:00

标签: javascript node.js http https request

我试图从我的localhost发送对我的localhost的请求,如下所示:

  http.get
    host: 'localhost:3000'
    path: '/events'
  , (response) ->
    console.log response

这导致此错误:

Error: getaddrinfo ENOTFOUND localhost:3000:80 localhost:3000:80:80
  at errnoException (dns.js:26:10)
  at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:77:26)

我无法弄清楚为什么会发生以及如何修复。我不能发送同一台服务器的请求吗?

感谢。

1 个答案:

答案 0 :(得分:1)

localhost:3000不是主持人;它是一个主机和一个端口。将它们作为单独的属性传递。

http.get
  host: 'localhost'
  port: 3000
  path: '/events'
, (response) ->
  console.log response