简体   繁体   English

为什么express-js不设置Content-Type标头?

[英]Why isn't express-js setting the Content-Type header?

I have the following: 我有以下内容:

var express = require('express'),
    app = express.createServer();

app.get("/offline.manifest", function(req, res){
  res.contentType("text/cache-manifest");
  res.end("CACHE MANIFEST");
});

app.listen(8561);

The network tab in Chrome says it's text/plain . Chrome中的网络标签显示为text/plain Why isn't it setting the header? 为什么不设置标题?

The code above works, my problems were caused by a linking to an old version of express-js 上面的代码有效,我的问题是由链接到旧版本的express-js引起的

res.type('json')现在也有效,正如其他人所说,你可以简单地使用
res.json({your: 'object'})

Try this code: 试试这段代码:

var express = require('express'),
    app = express.createServer();

app.get("/offline.manifest", function(req, res){
  res.header("Content-Type", "text/cache-manifest");
  res.end("CACHE MANIFEST");
});

app.listen(8561);

(I'm assuming you are using the latest release of express, 2.0.0) (我假设您使用的是最新版本的Express,2.0.0)

UPDATE: I just did a quick test using Firefox 3.6.x and Live HTTP Headers. 更新:我刚刚使用Firefox 3.6.x和Live HTTP Headers进行了快速测试。 This is the addons output: 这是插件输出:

 HTTP/1.1 200 OK
 X-Powered-By: Express
 Content-Type: text/cache-manifest
 Connection: keep-alive
 Transfer-Encoding: chunked

Make sure you clear your cache before trying. 确保在尝试之前清除缓存。

instead of res.send() 而不是res.send()

use res.json() which automatically sets the content-type to application/json 使用res.json()自动将content-type设置为application/json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM