how do you send html with restify

Problem

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

Problem courtesy of: MonkeyBonkey

Solution

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

Solution courtesy of: serg

View additional discussion.

“how do you send html with restify” Posted first on ” JavaScript on Medium “
Author: Node.js Recipes

Author: Pawan Kumar

Leave a Reply

Close Menu
%d bloggers like this:
Skip to toolbar