Kit Sunde
Kit Sunde

Reputation: 37075

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. 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

Upvotes: 30

Views: 68996

Answers (3)

Ray Hulha
Ray Hulha

Reputation: 11221

res.type('json') works too now and as others have said, you can simply use
res.json({your: 'object'})

Upvotes: 47

danday74
danday74

Reputation: 56956

instead of res.send()

use res.json() which automatically sets the content-type to application/json

Upvotes: 2

schaermu
schaermu

Reputation: 13460

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)

UPDATE: I just did a quick test using Firefox 3.6.x and 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.

Upvotes: 24

Related Questions