Skip to content

Instantly share code, notes, and snippets.

@ovaillancourt
Created March 24, 2012 17:55
Show Gist options
  • Save ovaillancourt/2185619 to your computer and use it in GitHub Desktop.
Save ovaillancourt/2185619 to your computer and use it in GitHub Desktop.
Wrapping the static mw to add a custom header entry.
var myStatic = express.static(__dirname + '/my/static/path');
app.use(function(req,res,next){
//We set the custom header here.
res.setHeader('My-header-name','my-header-value');
//We call the static middleware here, if the middleware calls "next", it means
//it didn't serve the file so we replace the static mw's "next" with a custom
//function that removes the header entry and calls the real "next" from the
//wrapper. This ensure we don't pollute subsequent middleware/route's response
//with that custom header.
myStatic(req,res,function(param){
res.removeHeader('My-header-name');
next(param);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment