how do you send html with restify

MonkeyBonkey picture MonkeyBonkey · Jun 11, 2012 · Viewed 13.6k times · Source

I want to send plain html instead of a json response for one of my routes in restify. I tried setting the contentType and header property of the response but it doesn't seem to set the contentType in the header (the browser tries to download the file rather than render it).

res.contentType = 'text/html';
res.header('Content-Type','text/html');
return res.send('<html><body>hello</body></html>');

Answer

serg picture serg · Mar 30, 2013

Quick way to manipulate headers without changing formatters for the whole server:

A restify response object has all the "raw" methods of a node ServerResponse on it as well.

var body = '<html><body>hello</body></html>';
res.writeHead(200, {
  'Content-Length': Buffer.byteLength(body),
  'Content-Type': 'text/html'
});
res.write(body);
res.end();