如何使用 Node.js 发出外部 HTTP 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7967037/
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
How to make external HTTP requests with Node.js
提问by Pierre
The question is fairly simple. I want to use a Node.js server as a proxy to log, authenticate, and forward HTTP queries to a backend HTTP server (PUT, GET, and DELETE requests).
这个问题相当简单。我想使用 Node.js 服务器作为代理来记录、验证和转发 HTTP 查询到后端 HTTP 服务器(PUT、GET 和 DELETE 请求)。
What library should I use for that purpose? I'm afraid I can't find one.
我应该为此目的使用什么库?恐怕我找不到一个。
回答by chovy
NodeJS supports http.request as a standard module: http://nodejs.org/docs/v0.4.11/api/http.html#http.request
NodeJS 支持 http.request 作为标准模块:http://nodejs.org/docs/v0.4.11/api/http.html#http.request
var http = require('http');
var options = {
host: 'example.com',
port: 80,
path: '/foo.html'
};
http.get(options, function(resp){
resp.on('data', function(chunk){
//do something with chunk
});
}).on("error", function(e){
console.log("Got error: " + e.message);
});
回答by hross
I would combine node-http-proxyand express.
我会结合node-http-proxy和express。
node-http-proxy will support a proxy inside your node.js web server via RoutingProxy(see the example called Proxy requests within another http server).
node-http-proxy 将通过以下方式支持您的 node.js Web 服务器中的代理RoutingProxy(请参阅在另一个 http 服务器中称为Proxy requests的示例)。
Inside your custom server logic you can do authentication using express. See the auth sample here for an example.
在您的自定义服务器逻辑中,您可以使用 express 进行身份验证。有关示例,请参见此处的auth 示例。
Combining those two examples should give you what you want.
结合这两个例子应该会给你你想要的。
回答by evilcelery
You can use the built-in httpmodule to do an http.request().
您可以使用内置http模块执行http.request().
However if you want to simplify the API you can use a module such as superagent
但是,如果您想简化 API,您可以使用诸如superagent 之类的模块
回答by Jamund Ferguson
node-http-proxy is a great solution as was suggested by @hross above. If you're not deadset on using node, we use NGINXto do the same thing. It works really well with node. We use it for example to process SSL requests before forwarding them to node. It can also handle cacheing and forwarding routes. Yay!
正如上面@hross 所建议的那样,node-http-proxy 是一个很好的解决方案。如果您不死心于使用 node,我们使用NGINX来做同样的事情。它与节点一起工作得很好。例如,我们使用它来处理 SSL 请求,然后再将它们转发到节点。它还可以处理缓存和转发路由。好极了!
回答by momo
You can use node.js http module to do that. You could check the documentation at Node.js HTTP.
您可以使用 node.js http 模块来做到这一点。您可以查看Node.js HTTP 中的文档。
You would need to pass the query string as well to the other HTTP Server. You should have that in ServerRequest.url.
您还需要将查询字符串传递给其他 HTTP 服务器。你应该在ServerRequest.url 中有它。
Once you have those info, you could pass in the backend HTTP Server and port in the options that you will provide in the http.request()
获得这些信息后,您可以将后端 HTTP 服务器和端口传递到您将在 http.request()

