node.js - 你如何用restify发送html

标签 node.js http-headers restify

我想在 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/

相关文章:

mysql - 如何用 Sail.js 和 MySQL 实现全文搜索?

javascript - Node.js - POST 到 iFrame?

javascript - 无法让按钮在 EJS 模板中工作

php - 缓存php解析的css、js、txt文件

PHP 文件服务脚本 : Unreliable Downloads?

php - 服务器上不允许 header ?

node.js - inversify-restify-utils Controller 中的空上下文和主体

linux - 应用程序 'appname' 无法在打开的 shift Node 应用程序上启动(端口 8080 不可用)

node.js - "Blocks your chain on reading and parsing the HTTP request body"是什么意思?

angularjs - 仅在单击 SideMenu 后才会显示 Ionic 内容