node.js 使用快速代理路由没有响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7559862/
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
No response using express proxy route
提问by Andreas K?berle
I've written a small proxy with nodejs, express and htt-proxy. It works well for serving local files but fails when it comes to proxy to external api:
我用 nodejs、express 和 htt-proxy 写了一个小代理。它适用于提供本地文件,但在代理到外部 api 时失败:
var express = require('express'),
app = express.createServer(),
httpProxy = require('http-proxy');
app.use(express.bodyParser());
app.listen(process.env.PORT || 1235);
var proxy = new httpProxy.RoutingProxy();
app.get('/', function(req, res) {
res.sendfile(__dirname + '/index.html');
});
app.get('/js/*', function(req, res) {
res.sendfile(__dirname + req.url);
});
app.get('/css/*', function(req, res) {
res.sendfile(__dirname + req.url);
});
app.all('/*', function(req, res) {
req.url = 'v1/public/yql?q=show%20tables&format=json&callback=';
proxy.proxyRequest(req, res, {
host: 'query.yahooapis.com', //yahoo is just an example to verify its not the apis fault
port: 8080
});
});
The problem is that there is no response from the yahoo api, maybe there is an response but i dont came up in the browser.
问题是雅虎 api 没有响应,也许有响应,但我没有在浏览器中出现。
回答by Stephan Hoyer
Even simpler with pipeand request-Package
使用pipe和request-Package更简单
var request = require('request');
app.use('/api', function(req, res) {
var url = apiUrl + req.url;
req.pipe(request(url)).pipe(res);
});
It pipes the whole request to the API and pipes the response back to the requestor. This also handles POST/PUT/DELETE and all other requests \o/
它将整个请求通过管道传递给 API,并将响应通过管道返回给请求者。这也处理 POST/PUT/DELETE 和所有其他请求 \o/
If you also care about query string you should pipe it as well
如果您还关心查询字符串,您也应该使用管道
req.pipe(request({ qs:req.query, uri: url })).pipe(res);
回答by hross
Maybe your code is different when you're testing, but I'm querying the same URL as in your code sample using the following:
也许您的代码在测试时有所不同,但我正在使用以下内容查询与您的代码示例中相同的 URL:
http://query.yahooapis.com:8080/v1/public/yql?q=show%20tables&format=json&callback=
http://query.yahooapis.com:8080/v1/public/yql?q=show%20tables&format=json&callback=
and I get nothing back. My guess is you want to change port to 80 (from 8080) -- it works when I change it like so:
我什么也得不到。我的猜测是您想将端口更改为 80(从 8080)——当我像这样更改它时它可以工作:
http://query.yahooapis.com:80/v1/public/yql?q=show%20tables&format=json&callback=
http://query.yahooapis.com:80/v1/public/yql?q=show%20tables&format=json&callback=
So that means it should be:
所以这意味着它应该是:
proxy.proxyRequest(req, res, {
host: 'query.yahooapis.com', //yahoo is just an example to verify its not the apis fault
port: 80
});
回答by Andreas K?berle
Maybe I use http-proxy in a wrong way. Using restler does what I want:
也许我以错误的方式使用了 http-proxy。使用restler做我想要的:
var express = require('express'),
app = express.createServer(),
restler = require('restler');
app.use(express.bodyParser());
app.listen( 1234);
app.get('/', function(req, res) {
console.log(__dirname + '/index.html')
res.sendfile(__dirname + '/index.html');
});
app.get('/js/*', function(req, res) {
res.sendfile(__dirname + req.url);
});
app.get('/css/*', function(req, res) {
res.sendfile(__dirname + req.url);
});
app.all('/*', function(req, res) {
restler.get('http://myUrl.com:80/app_test.php/api' + req.url, {
}).on('complete', function (data) {
console.log(data)
res.json(data)
});
});
回答by Ioanna
I ended up using http-proxy-middleware.
我最终使用了http-proxy-middleware。
The code looks something like this:
代码如下所示:
var express = require("express");
var proxy = require("http-proxy-middleware");
const theProxy = proxy({
target: "query.yahooapis.com",
changeOrigin: true,
});
app.use("/", theProxy);
app.listen(process.env.PORT || 3002);

