node.js - nodeJS 请求 POST 到本地主机 :3000 ECONNREFUSED

标签 node.js express post request econnrefused

有一次,我只想向在 localhost:3000 上运行的 rails 服务器发出 POST 请求

如果在 cUrl 上使用像 postman 这样的东西,我可以 ping 我的 API

但是,我想通过我的 Node 应用程序执行此操作:

    var req_body = {
       'my_id': id,
        'dataTable': data }


    var options = {
       method:   'POST',
       url:      config.get('api_url') + 'end_tracking',
       json:     true,
       body:     req_body
      }
      //  config.get('api_url') return http://localhost:3000/

      request(options, function (error, response, body) {
                console.log(error)
    ... }

这就是我得到的:

{ Error: connect ECONNREFUSED 127.0.0.1:3000
at Object.exports._errnoException (util.js:896:11)
at exports._exceptionWithHostPort (util.js:919:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1073:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3000 }

正如我所说,如果我直接向 postman 发出此请求,一切都会正常!

最佳答案

我终于做到了!

出于未知原因,访问使用 rails s 启动的 localhost:3000 在我的 Node 服务器上根本不起作用

但是当我使用rails s -b 0.0.0.0启动rails服务器时我工作了!

关于node.js - nodeJS 请求 POST 到本地主机 :3000 ECONNREFUSED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42142543/

相关文章:

node.js - axios 内请求/Express 上的 POST 请求

javascript - Heroku 在 heroku (Node.js) 上找不到本地模块

html - 我该如何解决 'Cannot set headers after they are sent to the client'

带有 XML 正文和文件作为附件的 HTTP POST

angularjs - 带有 $http 的 Access-Control-Allow-Headers 不允许请求 header 字段

node.js - 如何在亚马逊 EC2 Linux 实例上托管我的应用程序 (MEAN)?

javascript - 无法使用动态键访问 process.env 值

node.js - 在 HTML 属性中插入值 - Pug (Jade)

php - 如果当前表单中没有表单字段,如何在我的 POST 中包含表单字段?

node.js - 无法连接到 MongoDB。请确保 mongod 正在 mongodb 上运行 ://localhost:27017/mongo-server