使用快速代理路由无响应
我用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
});
});
问题是 yahoo api 没有响应,也许有响应,但我没有在浏览器中出现。
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:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用
pipe
和request
-Package 甚至更简单它将整个请求传送到 API 并将响应传送回请求者。这也处理 POST/PUT/DELETE 和所有其他请求 \o/
如果您也关心查询字符串,您也应该通过管道传递它
Even simpler with
pipe
andrequest
-PackageIt 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/
If you also care about query string you should pipe it as well
也许您在测试时的代码有所不同,但我使用以下命令查询与代码示例中相同的 URL:
http://query.yahooapis.com:8080/v1/public/yql?q=show%20tables&format=json&callback=
我什么也没得到。我的猜测是你想将端口更改为 80(从 8080)——当我像这样更改它时它就可以工作:
http://query.yahooapis.com:80/v1/public/yql?q=show%20tables&format=json&callback=
这意味着它应该是:
Maybe your code is different when you're testing, but I'm querying the same URL as in your code sample using the following:
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:
http://query.yahooapis.com:80/v1/public/yql?q=show%20tables&format=json&callback=
So that means it should be:
也许我以错误的方式使用http代理。使用 Restler 可以实现我想要的功能:
Maybe I use http-proxy in a wrong way. Using restler does what I want:
这就是我已经使用了一段时间的东西。可以处理 JSON 和二进制请求。
That's what I've been using for a while. Can handle both JSON and binary requests.
我最终使用了http-proxy-middleware。
代码看起来像这样:
I ended up using http-proxy-middleware.
The code looks something like this: