No response using express proxy route

后端 未结 5 900
执念已碎
执念已碎 2020-12-07 08:55

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 e         


        
相关标签:
5条回答
  • 2020-12-07 09:33

    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
    });
    
    0 讨论(0)
  • 2020-12-07 09:35

    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);
    
    0 讨论(0)
  • 2020-12-07 09:36

    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);
        }
    });
    
    0 讨论(0)
  • 2020-12-07 09:51

    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)
                });
    
    });
    
    0 讨论(0)
  • 2020-12-07 09:52

    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);
    
    0 讨论(0)
提交回复
热议问题