在NodeJS上重定向图像请求

时间:2016-05-20 14:25:34

标签: javascript node.js

在我的应用程序代码中,对于一组特定的API,我正在发出如下的NodeJS请求,它应该返回一个图像作为正文。这个相同的请求在Postman上工作正常(我可以看到图像)。

module.exports = {
    getThumbnail: function (thumbnailUrn, env, token, onsuccess){
        request({
            url: config.baseURL(env) + config.thumbail(thumbnailUrn),
            method: "GET",
            headers: {
                'Authorization': 'Bearer ' + token,
            }
        }, function (error, response, body) {
            // error check removed for simplicity...
            onsuccess(body);
        });
    }
}

上面的代码在我自己的安全检查下运行,并添加了令牌标头。它工作正常(请求调用返回200 / OK)。

现在在我的应用程序路由器上,我想将此作为图像进行响应,但输出不会被解释为图像。这就是我所拥有的:

var dm = require(/*the above code*/);

// express router
var express = require('express');
var router = express.Router();

router.get('/getThumbnail', function (req, res) {
    var urn = req.query.urn;
    dm.getThumbnail(urn, req.session.env, req.session.oauthcode, function (thumb) {
        res.writeHead(200,
            {
                'Content-Type': 'image/png'
            }
        );
        // at this point, the 'thumb' variable is filled
        // but I believe is not properly encoded...
        // or maybe the res.end output is missing something...
        res.end(thumb,  'binary');
    });
});

module.exports = router;

编辑:由Nodari Lipartiya评论,这是一种代理行为(服务器(用图像响应) - >代理(node.js /重新发送到客户端) - >最终用户)

2 个答案:

答案 0 :(得分:2)

我不确定thumb中会发生什么,但以下代码段似乎对我有用(为简单起见绕过Express):

var http = require("http")
var fs = require("fs")

var server = http.createServer(listener)

server.listen(() => {
    console.log(server.address().port)
})

var binary = fs.readFileSync("path to local image")

function listener(req, resp) {
    resp.writeHead(200,
            {
                'Content-Type': 'image/png'
            }
        );
    resp.end(new Buffer(binary), "binary")
}

如果将其换成Buffer

会怎样?

答案 1 :(得分:0)

如果我理解了一切: 我这样做了 的 server.js

var fs = require('fs');
var express = require('express');
var app = express();


app.get('/img', function(req, res, next) {
  var stream = fs.createReadStream('img.jpeg');
  var filename = "img.jpeg";
  filename = encodeURIComponent(filename);
  res.setHeader('Content-disposition', 'inline; filename="' + filename + '"');
  res.setHeader('Content-type', 'image/jpeg');
  stream.pipe(res);
});



app.listen(9999, function () {
  console.log('Example app listening on port 9999!');
});

<强>的proxy.js

var request = require('request');
var express = require('express');
var app = express();


app.get('/img', function(req, res, next) {
  console.log('proxy/img');
  request({
      url: 'http://localhost:9999/img',
      method: "GET",
  }, function (error, response, body) {
      res.end(body, 'binary');
  });
});



app.listen(9998, function () {
  console.log('Example app listening on port 9998!');
});

<强> req.js

var request = require('request');


request({
      url: 'http://localhost:9998/img',
      method: "GET",
  }, function (error, response, body) {
      console.log('body', body);
  });

适合我。如果您需要帮助,请告诉我。