I studied similar questions on SO, but couldn’t find a solution for my problem ... I set up an express route for serving images, but I can’t get it to return the image from where it was stored.Note, I turned on the instruction to allow requests from any source. It happens that when I make a request for http://localhost:8080/images/x10.png, I get an empty image element with src="http://localhost:8080/images/x10.pnginstead http://ubuntubox.dev/images/x10.png, which is where the image actually is, and this is the path that I ultimately pass to the request method. What am I missing? Thanks.
app.get('/images/*', function(req, res, path){
var imagePath = req.url,
url = 'http://ubuntubox.dev' + imagePath;
request(url, function(error, response, img) {
if(!error && response.statusCode === 200) {
res.header('Access-Control-Allow-Origin', '*');
res.writeHead(200, {'Content-Type': 'image/png' });
res.end(img, 'binary');
} else if(response.statusCode === 404) {
res.status(404);
res.type('txt').send('oops');
}
});
}).listen(8080, '127.0.0.1');
source
share