logo image

Setting max-age http response header for static files in NodeJS

Following NodeJS + Express code shows you how to set max-age for static content. Let's say you have static content in folders /public/images, /public/css, /public/js in the following code I am iterating on those folders and setting max-age for one day.

var express = require('express');
var app = express();

var oneDay = 86400000;
var staticResourceFolders = ['images', 'css', 'js'];

staticResourceFolders.forEach(function(staticResourceFolder) {
  app.use('/' + staticResourceFolder, express.static(__dirname + '/public/' + staticResourceFolder, {
    maxAge: oneDay