1.0.0 • Published 12 months ago
static-files v1.0.0
static-files
A simple node module for serving static files. Files are beeing cached for better performance.
usage
var http = require('http');
var sf = require('static-files');
http.createServer(function(request, response) {
var config = {
// root directory of your static files (here: folder "public" relative to "main.js")
rootPath : './public',
// paths can be mapped to files directly (path : filePath)
mappings : {
'/welcome' : '/index.html',
'/node/getIcon' : '/images/node.png'
}
// optional error handler (e.g. no file found for given path)
onError : function(response, error) {
response.setHeader('Content-Type', 'text/plain');
if (error.code === 'ENOENT') {
response.statusCode = 404;
response.end('Nothing found here...');
} else {
response.statusCode = 500;
response.end('Something went terribly wrong!!!');
}
}
};
// serve files via response for given configuration and request
sf.serve(request, response, config);
}).listen(3000);
console.log('Please browse to localhost:3000/welcome or localhost:3000/index.html');
console.log('Please browse to localhost:3000/node/getIcon or localhost:3000/images/node.png');