Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 384
Kit Sunde Avatar asked Mar 23 '11 03:03

Kit Sunde


People also ask

What is setHeader in node 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.

What is the purpose of content type header?

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.

What is var Express require (' Express ')?

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

Is Express JS unmaintained?

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 .


2 Answers

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

like image 180
Ray Hulha Avatar answered Oct 02 '22 12:10

Ray Hulha


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.

like image 45
schaermu Avatar answered Oct 02 '22 12:10

schaermu