how do you send html with restify


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';
return res.send('<html><body>hello</body></html>');
Problem courtesy of: MonkeyBonkey


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'
Solution courtesy of: serg


If you've overwritten the formatters in the restify configuration, you'll have to make sure you have a formatter for text/html. So, this is an example of a configuration that will send json and jsonp-style or html depending on the contentType specified on the response object (res):

var server = restify.createServer({
    formatters: {
        'application/json': function(req, res, body){
                var callbackFunctionName = req.params.callback.replace(/[^A-Za-z0-9_\.]/g, '');
                return callbackFunctionName + "(" + JSON.stringify(body) + ");";
            } else {
                return JSON.stringify(body);
        'text/html': function(req, res, body){
            return body;
Discussion courtesy of: dlawrence

It seems like the behaviour @dlawrence describes in his answer has changed since when the answer was posted. The way it works now (at least in Restify 4.x) is:

const app = restify.createServer(
    formatters: {
      'text/html': function (req, res, body, cb) {
        cb(null, body)
Discussion courtesy of: Stefano

Another option is to call


Instead of

Discussion courtesy of: Eran Boudjnah

This recipe can be found in it's original form on Stack Over Flow.