-
-
Save isaacs/3307869 to your computer and use it in GitHub Desktop.
node simple static router
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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