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

前端 未结 2 1085
独厮守ぢ
独厮守ぢ 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:03

    You can only write one value to a given header, so the second header is overwriting the first. Two solutions are

    1. write out a url to the image in the html - will be slightly slower for the user (needing an extra http request to fetch the image), but depending on the use case this is normally acceptable and very simple to implement
    2. convert the image to a data-uri string and include this in the html as the source of the image. More complicated to implement than the first approach ( I don't know of any libraries for doing the conversion in node) and with negligible benefits.

提交回复
热议问题