No response using express proxy route

后端 未结 5 902
执念已碎
执念已碎 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: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);
        }
    });
    

提交回复
热议问题