Headers not setting with redirect using node.js / Express Headers not setting with redirect using node.js / Express express express

Headers not setting with redirect using node.js / Express


I'd comment but I just joined today and don't have any reputation points.

If I understand it correctly, you're serving the page using express.static (so it's just a plain HTML page) but if the user is signed-in, you use express's router, am I correct?

I'm also guessing you put the mentioned code to set the headers in the homepage's route.

If that's the case, your issue isn't browser-caching, it occurs due to the nature of connect middlewares.

Middlewares execute in a chain, calling the next one when they're done, which means, if I assumed correctly, express.static is called prior to your router and it simply serve the static HTML page.

So your route is never executed because express.static won't call next() (for an obvious reason, the file exists).

Hope I assumed right.


Edit:

Looks like I assumed wrong. You did say it works fine on your laptop so it definitely looks like a client-side caching issue.

I'm still not sure how exactly you display a different homepage using express.static() or where you place the code you mentioned above, I'm going to give it a shot without seeing your code but it may be needed for me and others in order to help you out.

Those response headers should be set in the first response (when you visit the homepage), it has nothing to do with the redirect. Let's put the redirect part asides, for now.

I wrote a quick (express) example:

var express = require('express'),    http = require('http')    app = express();app.configure(function() {  app.set('port', process.env.PORT || 3000);  app.use(express.logger('dev'));  /*  * Here's where I set the headers, make sure it's above `express.static()`.  *   * Note: You can safely ignore the rest of the code, (it's pretty much "stock").  */  app.use(function noCachePlease(req, res, next) {    if (req.url === '/') {      res.header("Cache-Control", "no-cache, no-store, must-revalidate");      res.header("Pragma", "no-cache");      res.header("Expires", 0);    }    next();  });  app.use(express.static(__dirname + '/public'));});app.configure('development', function() {  app.use(express.errorHandler());});http.createServer(app).listen(app.get('port'), function() {  console.log("Express server listening on port " + app.get('port'));});

This code instructs my browser not to cache the page.

The response headers I get without the noCachePlease middleware:

Accept-Ranges   bytesCache-Control   public, max-age=0Connection  keep-aliveContent-Length  5Content-Type    text/html; charset=UTF-8Date    Fri, 20 Jul 2012 19:25:38 GMTEtag    "5-1342811956000"Last-Modified   Fri, 20 Jul 2012 19:19:16 GMTX-Powered-By    Express

The response headers I get with the noCachePlease middleware:

Accept-Ranges   bytesCache-Control   no-cache, no-store, must-revalidateConnection  keep-aliveContent-Length  5Content-Type    text/html; charset=UTF-8Date    Fri, 20 Jul 2012 19:26:08 GMTEtag    "5-1342811956000"Expires 0Last-Modified   Fri, 20 Jul 2012 19:19:16 GMTPragma  no-cacheX-Powered-By    Express

So as you can see, it works but you could run this code yourself.

If you want to run it, you'll need to have express under node_modules or installed globally (with the -g flag).