Render raw html in response with Express


I would like to know how to render a raw HTML string in a response with Express.

My question is different from the others because I am not trying to render a raw HTML template; rather I just want to render a single raw HTML string.

Here is what I have tried in my route file.

router.get('/myRoute', function (req, res, next) {
  var someHTML = "<a href=\"foo\">bar</a>"

But when I point my browser to this route, I see a hyperlink, instead of a raw HTML string. I have tried to set the content-type to text by doing: res.setHeader('Content-Type', 'text'); with no avail.

Any suggestions?


For others arriving here; this worked best for me:

res.set('Content-Type', 'text/html');
res.send(Buffer.from('<h2>Test String</h2>'));


And if your issue is escaping certain characters, then try using template literals: Template literals

Answered By – Richard Dunn

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published