how do you send html with restify how do you send html with restify node.js node.js

how do you send html with restify


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


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){            if(req.params.callback){                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;        }    }});


Another option is to call

res.end('<html><body>hello</body></html>');

Instead of

res.send('<html><body>hello</body></html>');