Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable webpage caching in ExpressJS + NodeJS?

Tags:

By default, my browser caches webpages of my ExpressJS app.

This is causing a problem to my login system (users not logged in can open old cached pages of logged in users).

How do I disable this caching?

EDIT:

My app.js (main file):

var express = require('express'); var http = require('http'); var path = require('path');  var store = require('./routes/store'); var app = express();  app.configure(function(){   app.set('port', process.env.PORT || 3012);   app.set('views', __dirname + '/views');   app.set('view engine', 'jade');   app.use(express.favicon());   app.use(express.logger('dev'));   app.use(express.bodyParser());   app.use(express.methodOverride());   app.use(express.cookieParser('your secret here'));   app.use(express.session());   app.use(app.router);   app.use(require('stylus').middleware(__dirname + '/public'));   app.use(express.static(path.join(__dirname, 'public'))); });  app.configure('development', function(){   app.use(express.errorHandler()); });  app.get('/', store.home); app.post('/', store.home);    app.get('/addProblem', store.addProblem); app.post('/addProblem', store.addProblem);  app.get('/problem', store.problem); app.post('/problem', store.problem);  app.get('/problemList', store.problemList); app.post('/problemList', store.problemList);  app.get('/main', store.main); app.post('/main', store.main);  app.post('/login', store.login); app.get('/login', store.login);  app.get('/createProblem', store.createProblem); app.post('/createProblem', store.createProblem);  app.post('/register', store.register); app.get('/register', store.register);  app.post('/evaluate', store.evaluate); app.get('/evaluate', store.evaluate);  app.get('/logout', store.logout); app.post('/logout', store.logout);  http.createServer(app).listen(app.get('port'), function(){   console.log("Express server listening on port " + app.get('port')); }); 
like image 788
batman Avatar asked Mar 25 '14 11:03

batman


People also ask

How do you stop Javascript from caching?

Answers. You can't prevent IE or for that matter any browser from caching Java Script file, you can only direct browser to always fetch latest version of Javascript file from the web server using the solution provided above. Tools->Internet Options. general tab->Settings and select the option "Every time ..."

What is the alternative of ExpressJS?

Koa, React, Flask, Django, and Golang are the most popular alternatives and competitors to ExpressJS.

What is caching in node JS?

Caching is the process of storing data in a high-speed storage layer so that future requests for such data can be fulfilled much faster than is possible through accessing its primary storage location.


2 Answers

There are two things to consider when dealing with cache in Express.js - ETag and Cache-Control headers.

ETag (MDN reference)
If you have dynamic content which does not benefit from ETags, it's best to disable it because it incurs small overhead with each request.

app.set('etag', false) 

Cache-Control (MDN reference)
To completely disable cache, use the following header:

app.use((req, res, next) => {   res.set('Cache-Control', 'no-store')   next() }) 

This header does not affect express.static() middleware. It handles cache in its own way.

like image 91
mike Avatar answered Sep 21 '22 06:09

mike


nocache

Don't waste your time reinventing the wheel, use the nocache middleware.

Install it:

npm install --save nocache 

Then add it to you app:

const nocache = require('nocache');  app.use(nocache()); 

It works in an easy way (from the doc):

This sets four headers, disabling a lot of browser caching:

  • Cache-Control: no-store, no-cache, must-revalidate, proxy-revalidate
  • Pragma: no-cache
  • Expires: 0
  • Surrogate-Control: no-store

Etag

Beware of ETags: this header isn't removed using the middleware above, because it works in a different way. It's generated at the end of the request, so you have two choices.

app.set

Disabling it using express builtin app.set('etag', false); method

on-headers

Removing the header just before it is sent to the client using the on-headers module:

const onHeaders = require('on-headers');  // install it as a middleware app.use((req, res, next) => {     // listen for the headers event     onHeaders(res, () => {         this.removeHeader('ETag');     }); }); 
like image 23
lifeisfoo Avatar answered Sep 19 '22 06:09

lifeisfoo