static_file_handler
v0.0.41
Published
static_file_hander ====
Downloads
10
Readme
static_file_hander
Point an http server to this to handle serving files from a folder, with ETag support.
Perhaps the best thing about this solution is it handles on the fly checking of file time and size (using fs.lstat) and requires no additional npms.
Instalation
npm install --save static_file_hander
Examples
A brief single statement example would be:
var server = require('http').createServer(require('static_file_handler').getStaticFileHandler({
paths:{
default:"./html/readme.html"}
})
).listen(8080);
Paths can contain different folders based on extension (typically used to specify a different location for scripts)
You must specify a default, which can either be a filename (meaning that's the home page, and it's in the root web folder), or a folder name (it will look for one of index.html,index.htm,home.html,home.htm, in that folder).
If you specify a favicon, it and the file does not exist, it will be replaced with a default icon that's in the html folder of the demo that ships with the source.
A complex example might be
var statics = require('static_file_handler');
var server = require('http').createServer(statics.getStaticFileHandler({
ramcache: {
"/hello.world": {
headers: {
"200": {
ETag: "nerds.rule"
}
},
content: "hello world",
mimeType: "text/plain"
},
"/random": {
headers: {
"200": {}
},
content: function () {
return Math.random().toString()
},
mimeType: "text/plain"
}
},
paths: {
favicon: "./html/image/mysite.ico",
js: {
files: "./js/",
cache: "./jscache/"
},
default: "./html/readme.html"
},
handlers: {
"200": function (req, res, toSend, stat) {
// do something here with toSend.path, toSend.headers, examine stat
// optionally set toSend.content, which will force a one time send of something other than the file.
// (unless it happens to be an entry you have defined in ramcache, in which case the content will be persistent.
// until you change it again.
// the "includedfile" npm uses this to unlify, and merge dependant files on the fly, keeping the result in a cache folder.
// if we updated the file and it's date and or size has changed, call this to
// rebuild the ETags (otherwise client may not get updated version as it's etag will match
// what's currently in toSend.
statics.defaultRefreshCacheEntry(toSend, stats);
return statics.default200Handler(req, res, toSend, stats);
}
}
})).listen(8080);