在 HTTP 请求中指定端口号 (node.js)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31662411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Specify port number in HTTP request (node.js)
提问by djfdev
Is it possible to specify a port number when making at HTTP request using the request module? I'm not seeing anything about this in the documentation:
是否可以在使用请求模块进行 HTTP 请求时指定端口号?我在文档中没有看到任何关于此的内容:
var request = require('request');
// this works
request({
method: 'GET',
url: 'http://example.com'
}, function(error, response, body) {
if (error) console.log(error);
console.log(body);
});
// this does not work
request({
method: 'GET',
url: 'http://example.com:10080'
}, function(error, response, body) {
// ...
});
Additionally, when I run the second version, absolutely nothing happens in my program (almost like the request was never made).
此外,当我运行第二个版本时,我的程序中绝对没有任何反应(几乎就像从未发出请求一样)。
I also know that I can specify a port number when making a request using the core httpmodule. Why is the not an option in the request module?
我也知道我可以在使用核心http模块发出请求时指定端口号。为什么不是请求模块中的选项?
EDIT: I should have mentioned this before, but I am running this application on Heroku.
编辑:我之前应该提到过这一点,但我正在 Heroku 上运行这个应用程序。
When I run the request locally (using the request module) I am able to specify the port number, and get a successful callback.
当我在本地运行请求时(使用请求模块),我能够指定端口号,并获得成功的回调。
When I run the request from Heroku, no callback is fired, and nginx shows no record of the request.
当我从 Heroku 运行请求时,没有触发回调,并且 nginx 没有显示请求的记录。
Am I crazy? Is there some reason Heroku is preventing me from making an outbound HTTP request to a specific port number?
我疯了吗?Heroku 是否有某种原因阻止我向特定端口号发出出站 HTTP 请求?
回答by Robin Murphy
Using requestwith the complete URL as the first argument works for me:
使用request完整的 URL 作为第一个参数对我有用:
var http = require('http');
var request = require('request');
// start a test server on some non-standard port
var server = http.createServer(function (req, res) {
res.end('Hello world');
});
server.listen(1234);
// make the GET request
request('http://127.0.0.1:1234', function (err, res) {
if (err) return console.error(err.message);
console.log(res.body);
// Hello world
server.close();
});
Specifying the methodand the urlseparately also works:
分别指定method和url也有效:
request({
method: 'GET',
url: 'http://127.0.0.1:1234'
}, function (err, res) {
if (err) return console.error(err.message);
console.log(res.body);
// Hello world
server.close();
});
You might want to check that your server is running and that you're not behind a proxy or firewall that prevents access on that port.
您可能想要检查您的服务器是否正在运行,并且您没有在阻止访问该端口的代理或防火墙后面。
回答by Mauro Moreno
I realize the question asks for the request module but in a more general context, if using the http module, you can use the portkey docs.
我意识到问题要求请求模块,但在更一般的上下文中,如果使用 http 模块,则可以使用port关键docs。
e.g.
例如
http.get({
host: 'example.com',
path: '/some/path/',
port: 8080
}, function(resp){
resp.on('data', function(d){
console.log('data: ' + d)
})
resp.on('end', function(){
console.log('** done **')
})
}).on('error', function(err){
console.log('error ' + err)
})

