johnnys-node-static

My own version of node-static based on the original Node module.

johnnys-node-static

Simplified version of node-static module.

Function Description Example
setStaticServer Sets the static server.
setRoutes Sets the routes object.
exists Verifies if the route does exist and returns true or false.
serve Serves the file specified in routes object.
serveAll Serves any file from root.

File structure:

root/
├── index.js
└── public/
    └── html/
        ├── index.html
        ├── test1.html
        └── test2.html

For the file structure above, the following routes would serve files for each url:

{
    "/":       { "url": "/html/index.html" },
    "/test1/": { "url": "/html/test1.html" },
    "/test2/": { "url": "/html/test2.html" }
}

This is the content for index.js file.

// require Johnny's static
var JohnnysStatic = require("johnnys-node-static"),
    http = require('http');
 
// set static server: public folder
JohnnysStatic.setStaticServer({root: "./public"});
 
// set routes
JohnnysStatic.setRoutes({
    "/":       { "url": "/html/index.html" },
    "/test1/": { "url": "/html/test1.html" },
    "/test2/": { "url": "/html/test2.html" }
});
 
// create http server
http.createServer(function(req, res) {
    // safe serve
    if (JohnnysStatic.exists(req, res)) {
        // serve file
        JohnnysStatic.serve(req, res, function (err) {
            // not found error
            if (err.code === "ENOENT") {
                res.end("404 - Not found.");
                return;
            }
 
            // other error
            res.end(JSON.stringify(err));
        });
        return;
    }
 
    // if the route doesn't exist, it's a 404!
    res.end("404 - Not found");
}).listen(8000);
npm install johnnys-node-static
npm test # or ./test.sh
  • Fixed route setting.
  • Fixed the bug related to the status code.
  • Added serveAll method.
  • Initial release.

See LICENCE file.