This is a small hapi module to allow you to serve files and directories from a local path mapped to a url.
Simply give it a local path and a remote base url
var electricfenceConfig = {
path: 'public',
url: '/',
listing: true,
index: false,
cache: 3600000
};
plugin.require({electricfence: electricfenceConfig});
Those are the defaults, so if you pass it nothing those will be used.
path
(string, optional, defaultpublic
): folder to serve files fromurl
(string, optional, default/
): url to serve files atlisting
(boolean, defaulttrue
): determines if 'index.html' will be served if found in the folder when - requesting a directoryindex
(boolean, defaultfalse
): determines if directory listing is generated when a directory is requested without an index documentcache
(integer, default3600000
(one hour)): time in milliseconds to tell the browser to cache results. Set to 0 to disable browser caching headers
"But Gar," you say, "hapi already has directory and file handlers!"
Yes, but if your server already has a catchall route such as:
server.route({
method: 'get',
path: '/{posts*}',
handler: postHandler
});
You can't then add
server.route({
method: 'get',
path: '/{path*}',
handler: {directory: {path: 'public'}}
});
The paths will conflict. What electricfence allows you to do is just this.
electricfence adds explicit file and directory handlers for anything that's in the local path you give it. This means for example if you have a js
and css
directory in ./public
, and you also have a robots.txt
file, electricfence will add these handlers
server.route({
method: 'get',
path: '/css/{path*}',
handler: {directory: {path: 'public/css'}}
});
server.route({
method: 'get',
path: '/js/{path*}',
handler: {directory: {path: 'public/js'}}
});
server.route({
method: 'get',
path: '/robots.txt',
handler: {file: {path: 'public/robots.txt'}}
});