Nodejs: How return different content types with same response (text and image)?

前端 未结 2 1076
独厮守ぢ
独厮守ぢ 2020-12-16 21:15

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

2条回答
  •  自闭症患者
    2020-12-16 22:01

    If you do response.write(''); 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 :

    
    

    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('');//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'});

提交回复
热议问题