小编Kar*_*mia的帖子

在节点中创建 REST API 时,如何将 http 响应从对外部网站发出的请求流式传输到原始 api 调用?

所以,我正在使用 node 创建一个 REST API,我必须创建一个路由。路由的目的:充当代理服务器并调用不同的外部网站并返回它对原始请求的响应。到目前为止,我有以下代码并且它有效:

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)
        })
    }
)
Run Code Online (Sandbox Code Playgroud)

我的问题是,如何流式传输从外部网站获得的这个 http 响应。我的意思是,我想以流的形式获取响应,并在它以块的形式出现时立即返回它。这可能吗?

api rest http node.js http-streaming

5
推荐指数
1
解决办法
2582
查看次数

标签 统计

api ×1

http ×1

http-streaming ×1

node.js ×1

rest ×1