Skip to content

Instantly share code, notes, and snippets.

@jfromaniello
Last active August 29, 2015 13:58
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save jfromaniello/10021643 to your computer and use it in GitHub Desktop.
Save jfromaniello/10021643 to your computer and use it in GitHub Desktop.
function onlyStatic (middleware) {
return function (req, res, next) {
var match = /(\.css|\.eot|\.gif|\.html|\.js|\.png|\.svg|\.ttf|\.woff|\.jpg)($|\?.*$)/ig.exec(req.originalUrl);
if (!match) return next();
middleware(req, res, next);
};
}
//usage
this.use(onlyStatic(express.static(__dirname + "/public")));
@molokoloco
Copy link

You don't like JPG ? :)

@jfromaniello
Copy link
Author

@molokoloco haha good catch

@jfromaniello
Copy link
Author

Actually this regex fails if you have a request like:

/foo?file=x.html << this is not static

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment