I\'m trying to learn nodejs and I thought the best way to do this would be to try doing some stuff without using express or any other non-core modules. I\'m stuck on trying
If you do response.write('<img src="my_pic.jpg"/>');
as mentioned above the image file would be sent only when browser sends GET for the image. It would become multi-part request.
Or you can do it like this. It is possible to send your image in binary form in HTML. Use :
<img src="data:image/gif;base64,imagedata">
where imagedata is a base64 encoding of gif image. So do this in node.js :
//Write text in response.
content = get-image-file-contents; //store image into content
imagedata = new Buffer(content).toString('base64'); //encode to base64
response.write('<img src="data:image/gif;base64,'+imagedata+'">');//send image
response.end();
Check here for correct image conversion NodeJS base64 image encoding/decoding not quite working
This sends one response which sends both text and image. Only one header is required for response response.writeHead(200, {'content-type':'text/html'});
You can only write one value to a given header, so the second header is overwriting the first. Two solutions are