0.1.3 • Published 10 years ago

johnnys-node-static v0.1.3

Weekly downloads
1
License
MIT
Repository
github
Last release
10 years ago

johnnys-node-static

Simplified version of node-static module.

npm.io

Documentation

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 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);

Test

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

Changelog

v0.1.3

  • Fixed route setting.

v0.1.2

  • Fixed the bug related to the status code.

v0.1.1

  • Added serveAll method.

v0.1.0

  • Initial release.

Licence

See LICENCE file.