在 ExpressJS 中管道远程文件 [英] Piping remote file in ExpressJS

查看:19
本文介绍了在 ExpressJS 中管道远程文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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();
  });
});

推荐答案

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

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

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

这篇关于在 ExpressJS 中管道远程文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆