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

Get request using http module for localhost/same host

本文关键字:主机 http 模块 请求 获取      更新时间:2023-09-26

我正试图从本地主机发送一个请求,如下所示:

  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)

我搞不清楚为什么会发生这种情况以及如何解决。我不能发送对同一服务器的请求吗?

谢谢。

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

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