使用快速代理路由无响应

发布于 2024-12-06 14:39:05 字数 888 浏览 1 评论 0原文

我用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

困倦 2024-12-13 14:39:05

使用 piperequest-Package 甚至更简单

var request = require('request');

app.use('/api', function(req, res) {
  var url = apiUrl + req.url;
  req.pipe(request(url)).pipe(res);
});

它将整个请求传送到 API 并将响应传送回请求者。这也处理 POST/PUT/DELETE 和所有其他请求 \o/

如果您也关心查询字符串,您也应该通过管道传递它

req.pipe(request({ qs:req.query, uri: url })).pipe(res);

Even simpler with pipe and 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/

If you also care about query string you should pipe it as well

req.pipe(request({ qs:req.query, uri: url })).pipe(res);
债姬 2024-12-13 14:39:05

也许您在测试时的代码有所不同,但我使用以下命令查询与代码示例中相同的 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=

这意味着它应该是:

proxy.proxyRequest(req, res, {
    host: 'query.yahooapis.com', //yahoo is just an example to verify its not the apis fault
    port: 80
});

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:

proxy.proxyRequest(req, res, {
    host: 'query.yahooapis.com', //yahoo is just an example to verify its not the apis fault
    port: 80
});
日记撕了你也走了 2024-12-13 14:39:05

也许我以错误的方式使用http代理。使用 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)
            });

});

Maybe I use http-proxy in a wrong way. Using restler does what I want:

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)
            });

});
爺獨霸怡葒院 2024-12-13 14:39:05

这就是我已经使用了一段时间的东西。可以处理 JSON 和二进制请求。

app.use('/api', (req, res, next) => {
    const redirectUrl = config.api_server + req.url.slice(1);
    const redirectedRequest = request({
        url: redirectUrl,
        method: req.method,
        body: req.readable ? undefined : req.body,
        json: req.readable ? false : true,
        qs: req.query,
        // Pass redirect back to the browser
        followRedirect: false
    });
    if (req.readable) {
        // Handles all the streamable data (e.g. image uploads)
        req.pipe(redirectedRequest).pipe(res);
    } else {
        // Handles everything else
        redirectedRequest.pipe(res);
    }
});

That's what I've been using for a while. Can handle both JSON and binary requests.

app.use('/api', (req, res, next) => {
    const redirectUrl = config.api_server + req.url.slice(1);
    const redirectedRequest = request({
        url: redirectUrl,
        method: req.method,
        body: req.readable ? undefined : req.body,
        json: req.readable ? false : true,
        qs: req.query,
        // Pass redirect back to the browser
        followRedirect: false
    });
    if (req.readable) {
        // Handles all the streamable data (e.g. image uploads)
        req.pipe(redirectedRequest).pipe(res);
    } else {
        // Handles everything else
        redirectedRequest.pipe(res);
    }
});
残花月 2024-12-13 14:39:05

我最终使用了http-proxy-middleware

代码看起来像这样:

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);

I ended up using 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文