gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:55:34 25 4
gpt4 key购买 nike

有一次,我只想向在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com