Skip to content

Instantly share code, notes, and snippets.

@Naouak
Last active December 16, 2015 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Naouak/5491699 to your computer and use it in GitHub Desktop.
Save Naouak/5491699 to your computer and use it in GitHub Desktop.
Serving static content on dynamic url with expressjs
//Create a module for static middleware instanciation with a singleton
//Use the same singleton for app.use and variable routing.
//If there is some caching stuff going on, they should share the same cache which is a lot better.
var express = require("express");
var static = null;
module.exports = function(){
if(!static){
static = express.static(__dirname+"/public");
}
return static;
}
module.exports = function(req,res,next){
var static = express.static(__dirname+"/public");
req.method = "GET";
req.url = "/mystaticcontent.html";
static(req,res,next);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment