[英]nodeJS request POST To localhost:3000 ECONNREFUSED
At one point I just want to make a POST
request to my rails
server running on localhost:3000
有一次我只想向在
localhost:3000
上运行的rails
服务器发出POST
请求
If a use like postman
on even cUrl
I can ping my API如果像
postman
一样在cUrl
上使用,我可以 ping 我的 API
But kow, with my node app I want to do this:但是,哇,我想用我的节点应用程序来做到这一点:
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)
... }
this is what I get:这就是我得到的:
{ 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 }
and as I said, if I do this request directly with postman, everything works !正如我所说,如果我直接向邮递员提出这个请求,一切正常!
I finally did it !我终于做到了!
For a unknown reason, accessing localhost:3000
, launched with rails s
didn't work at all on my node server出于未知原因,访问
localhost:3000
,使用rails s
启动在我的节点服务器上根本不起作用
but I worked when I started the rails server with rails s -b 0.0.0.0
!但是当我用
rails s -b 0.0.0.0
启动 rails 服务器时我工作了!
how i did this, just use the same structure and i hope it works for you我是怎么做到的,只是使用相同的结构,我希望它对你有用
request({
url: url, //URL to hit
method: 'post',
headers: headers,
timeout: 10000,
body: JSON.stringify(body)
}, function (error, result, body) {
if (error) {
console.log(error);
} else if (result.statusCode == 500) {
console.log('not found');
} else {
console.log('success')
}
});
hope it works.希望它有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.