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
setHeader(name, value) (Added in v0. 4.0) method is an inbuilt application programming interface of the 'http' module which sets a single header value for implicit headers. If this header already exists in the to-be-sent headers, its value will be replaced.
The Content-Type representation header is used to indicate the original media type of the resource (prior to any content encoding applied for sending). In responses, a Content-Type header provides the client with the actual content type of the returned content.
var express = require('express'); => Requires the Express module just as you require other modules and and puts it in a variable. var app = express(); => Calls the express function "express()" and puts new Express application inside the app variable (to start a new Express application).
It is unmaintained Express has not been updated for years, and its next version has been in alpha for 6 years. People may think it is not updated because the API is stable and does not need change. The reality is: Express does not know how to handle async/await .
res.type('json')
works too now and as others have said, you can simply useres.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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With