Express-如何从远程服务器提供CSV?

我有一个路由的简单节点/ express API。在此路由中,我想向发送回CSV的第三方服务发出API请求。反过来,我想发送该CSV作为响应。

我有这个,但感觉到我可能缺少一些基本的东西:

//get csv from remote server
let csv = await axios.get('https://api.com/csv',{
    responseType: 'blob'
})
//serve that csv as a response
res.send(csv)
tl_tl2009 回答:Express-如何从远程服务器提供CSV?

您必须像这样使用axios。

    axios.get('https://api.com/csv',{
      responseType: 'blob'
     })
      .then(function (response) {
        // handle success
        res.send(response);
      })

有关更多信息,请参阅此https://www.npmjs.com/package/axios

,

您可以将管道用于httpStream。如果文件大小很大,它将非常快。这是使用nodejs内置的http模块。希望对您有帮助。

app.get('/path',(req,res)=>{
var options = {
    hostname: 'hostname',port: 80,path: '/path',method: 'GET',headers: {

    }
  };

  var req = http.request(options,(response) => {

    response.pipe(res).on('error',(error)=>{
        console.log('Error');
    });
  });

  req.on('error',(e) => {
    console.error(`problem with request: ${e.message}`);
  });
  req.end();
});
本文链接:https://www.f2er.com/3148949.html

大家都在问