Skip to content

Releases: IonicaBizauTrash/johnnys-node-static

Added `serveAll` method

06 Oct 06:03
Compare
Choose a tag to compare

johnnys-node-static

Simplified version of node-static module.

Documentation

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.

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.1

  • Added serveAll method.

v0.1.0

  • Initial release.

Licence

See LICENCE file.

Initial release

06 Oct 05:18
Compare
Choose a tag to compare

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.