Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
27
rated 0 times [  32] [ 5]  / answers: 1 / hits: 57315  / 9 Years ago, tue, february 10, 2015, 12:00:00

My Node.js server has something that looks like the following:



app.get(/api/id/:w, function(req, res) {
var data = getIcon(req.params.w);
});


Here, data is a string containing a Base64 representation of a PNG image. Is there any way I can send this to a client accessing the route encoded and displayed as an image (e.g. so the URL can be used in an img tag)?


More From » node.js

 Answers
168

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

[#67881] Saturday, February 7, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cherish

Total Points: 734
Total Questions: 94
Total Answers: 86

Location: Senegal
Member since Fri, Aug 21, 2020
4 Years ago
;