Skip to content

Instantly share code, notes, and snippets.

@isaacs
Forked from max-mapper/readme.md
Created August 9, 2012 20:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save isaacs/3307869 to your computer and use it in GitHub Desktop.
Save isaacs/3307869 to your computer and use it in GitHub Desktop.
node simple static router
var st = require('st')
var mount = st({
path: 'resources/static/',
url: 'static/', // defaults to path option
cacheSize: 1024*1024*1024, // 1GB memory cache. default = 0
fdCacheSize: 1024, // number of fd's to cache. default = 1024
index: true, // show an html index for dirs
index: 'index.html', // use index.html for dirs, if present
cacheExpiry: 10*60*1000, // expiration of contentCache in ms
serveStale: true // update in the background,
})
// with bare node.js
http.createServer(function (req, res) {
if (mount(req, res)) return // serving a static file
myCustomLogic(req, res)
}).listen(PORT)
// with express
app.use(mount)
// or
app.route('/static/:fooblz', function (req, res, next) {
mount(req, res, next) // will call next() if it doesn't do anything
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment