javascript 在 ExpressJS 中管道远程文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18432779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 11:54:28  来源:igfitidea点击:

Piping remote file in ExpressJS

javascriptnode.jsexpress

提问by cyberwombat

I would like to read a remote image and display it. I can save the file but not getting the code right to display it. Ideally I just want to pass the file right though without processing - not sure if a tmp file step is required or not. This code displays nothing - no errors. I tried res.pipe(response) as well.

我想读取远程图像并显示它。我可以保存文件,但没有正确显示代码。理想情况下,我只想在不处理的情况下直接传递文件 - 不确定是否需要 tmp 文件步骤。此代码不显示任何内容 - 没有错误。我也试过 res.pipe(response) 。

var url = 'http://proxy.boxresizer.com/convert?resize=50x50&source=' + filename

var request = http.get(url, function(response) {

  var tmp = path.join(require('os').tmpDir(), filename);

  var outstream = require('fs').createWriteStream(tmp);

  response.pipe(outstream);
  response.on('end', function() {
    res.set('Content-Type', 'image/jpg');
      res.pipe(outstream);
      res.end();
  });
});

回答by cyberwombat

Well I'd still like to know how to make the above work but I solved my issue in one line with the requestmodule!

好吧,我仍然想知道如何进行上述工作,但我在请求模块的一行中解决了我的问题!

var url = 'http://proxy.boxresizer.com/convert?resize=50x50&source=' + filename
require('request').get(url).pipe(res);  // res being Express response