Redirect image request on NodeJS
<p>Inside my application code, for a specific set of APIs, I'm making a NodeJS request like following, which should return a image as the body. This same request works fine on Postman (and I can see the image).</p>

<pre><code>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);
        });
    }
}
</code></pre>

<p>The above code run under my own security checks and adds the token header. It works fine (request calls return 200/OK).</p>

<p>Now on my app router I want to respond this as an image, but the output is not being interpreted as an image. Here is what I have:</p>

<pre><code>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;
</code></pre>

<p><strong>EDIT</strong>: as commented by <a href="https://stackoverflow.com/users/2258438/nodari-lipartiya">Nodari Lipartiya</a>, this is kind of proxy behaviour ( server(responds with image) -> proxy (node.js/resends to client) -> end user)</p>