簡體   English   中英

使用快速代理路由沒有響應

[英]No response using express proxy route

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

});

問題是雅虎 api 沒有響應,也許有響應,但我沒有在瀏覽器中出現。

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

也許您的代碼在測試時有所不同,但我正在使用以下內容查詢與您的代碼示例中相同的 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
});

也許我以錯誤的方式使用了 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)
            });

});

我最終使用了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);

這就是我一直在使用的一段時間。 可以處理 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);
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM