我想在 restify 中为我的一条路由发送纯 html 而不是 json 响应。我尝试设置响应的 contentType 和 header 属性,但它似乎没有在 header 中设置 contentType(浏览器尝试下载文件而不是渲染它)。
res.contentType = 'text/html';
res.header('Content-Type','text/html');
return res.send('<html><body>hello</body></html>');
最佳答案
无需更改整个服务器的格式化程序即可快速操作 header :
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();
关于node.js - 你如何用restify发送html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10973479/