30

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

MPelletier
  • 16,256
  • 15
  • 86
  • 137
Kit Sunde
  • 35,972
  • 25
  • 125
  • 179

3 Answers3

47

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

Ray Hulha
  • 10,701
  • 5
  • 53
  • 53
  • 2
    this is great since it still allows you to use `res.send(obj)` to send objects as JSON. Better than `res.end(JSON.stringify(obj))` – Joseph Nields Mar 17 '17 at 04:37
  • 1
    you can chain it too, `res.type('json').send({your: 'object'});` or as @danday74 below points out, simply `res.json({your: 'object'});` – Steve Kehlet Feb 27 '18 at 02:58
24

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.

schaermu
  • 13,478
  • 2
  • 39
  • 32
2

instead of res.send()

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

danday74
  • 52,471
  • 49
  • 232
  • 283