简体   繁体   中英

While creating a REST API in node, How can I stream http response from a request made to an external website to the original api call?

So, I am creating a REST API using node and I have to create a route. Purpose of the route: Act as a proxy server and make a call to a different external website and return the response it gets to the original request. So far, I have the following code and it works:

app.post('/v1/something/:_id/proxy',
    function(req, res, next) {
        // Basically make a request call to some external website and return
        // the response I get from that as my own response
        var opts = {/*json containing proper uri, mehtod and json*/}
        request(opts, function (error, responseNS, b) {
            if(error) return callback(error)
            if(!responseNS) return callback(new Error('!response'))

            return res.json(responseNS.body)
        })
    }
)

My question is, how can I stream this http response that I am getting from the external website. By that, I mean that I want to get the response as a stream and keep returning it as soon as it comes in chunks. Is this possible?

You can pipe the incoming response from an external source straight to a response that your app sends to the browser, like this:

app.post('/v1/something/:_id/proxy',
function(req, res, next) {
    // Basically make a request call to some external website and return
    // the response I get from that as my own response
    var opts = {/*json containing proper uri, mehtod and json*/}
    request(opts, function (error, responseNS, b) {
        if(error) return callback(error)
        if(!responseNS) return callback(new Error('!response'))

        return res.json(responseNS.body)
    }).pipe(res);
});

With request you can directly pipe incoming response to either file stream, to other requests or to the response that your api sends to the browser. Like

function (req, res, next) {
    request
      .get('http://example.com/doodle.png')
      .pipe(res)    
}

Similary in your case just pipe to response.

app.post('/v1/something/:_id/proxy',
    function(req, res, next) {
        // Basically make a request call to some external website and return
        // the response I get from that as my own response
        var opts = {/*json containing proper uri, mehtod and json*/}
        request(opts, function (error, responseNS, b) {
            if(error) return callback(error)
            if(!responseNS) return callback(new Error('!response'))
        }).pipe(res);
    }
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM