Rendering a Base64 PNG with Express
Yes you can encode your base64 string and return it to the client as an image:
server.get("/api/id/:w", function(req, res) {
var data = getIcon(req.params.w);
var img = Buffer.from(data, 'base64');
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': img.length
});
res.end(img);
});
Using "base64-img" component:
app.get('/image1', function(req, res) {
var image1 = 'image1.jpg';
var base64Img = require('base64-img');
var imageData1 = base64Img.base64Sync(image1);
var base64Data = imageData1.replace(/^data:image\/(png|jpeg|jpg);base64,/, '');
var img = Buffer.from(base64Data, 'base64');
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': img.length
});
res.end(img);
});
I had to do a bit of manipulation first to get mine in the right format, but this worked great:
var base64Data = data.replace(/^data:image\/png;base64,/, '');