Skip to content

Initial release

Compare
Choose a tag to compare
@IonicaBizau IonicaBizau released this 06 Oct 05:18
· 23 commits to master since this release

johnnys-node-static

Simplified version of node-static module.

Documentation

Function Description
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.

Example

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 JohhnysStatic = require("../index.js"),
    http = require('http');

// set static server: public folder
JohhnysStatic.setStaticServer({root: "./public"});

// set routes
JohhnysStatic.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 (JohhnysStatic.exists(req, res)) {
        // serve file
        JohhnysStatic.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);

Test

npm install johnnys-node-static
npm test # or ./test.sh

Changelog

v0.1.0

  • Initial release.

Licence

See LICENCE file.