Giter Site home page Giter Site logo

nice-router's Introduction

nice-router

A nice, simpler router for handling those pesky HTTP(S) requests.

Example

var Router = require("nice-router");
var router = new Router();
router.addRoute("/ping", "GET", function(req, res) {
    res.writeHead(200);
    res.end("pong");
});
router.addStatic("/smiley.jpg", "image/jpeg");
router.listen(8080);

Basic HTTPS example

To use this example you will need to generate self-signed certificates server.key and server.crt. If you are unsure about how to do that, see this Stack Overflow.

var fs = require("fs");
var Router = require("nice-router");
var router = new Router();
router.useHTTPS({
    key: fs.readFileSync("key.pem"),
    cert: fs.readFileSync("cert.pem"),
};
router.addRoute("/amIEncrypted", "GET", function(req, res) {
    res.writeHead(200);
    res.end("yes!");
});
router.listen(8080);

Methods

  • #addRoute(path, method, handler): Adds a route to the router which will respond to method requests to path by doing handler. The signature of handlers is handler(request, response, headers, query, body). The request and response objects are the same as those from the node http/https modules. headers and query are basic key/value objects. body is a Buffer, so call body.toString("utf-8") if you just want a string.
  • #listen(port) -> Promise: Start the server on the specified port, which must be open.
  • #close -> Promise: Stop the server.
  • #useHTTPS(options[, callback]): Switches the server to using HTTPS, restarting if the server was already running. options is the same as the options parameter in node's https.createServer. Accepts an optional callback which will be called if the server is restarted.
  • #addStatic(path, contentType): Adds a handler which responds to GET requests at path with the contents of the file at path.

nice-router's People

Contributors

tdb-alcorn avatar

Watchers

James Cloos avatar  avatar

nice-router's Issues

Handler signature is a tad awkward.

Instead of handler(request, response, body, headers, query) it should be handler(request, response, headers, query, body). This is so that handlers GET requests (and other requests with no body), can just use the shorthand handler(request, response, [headers, query]) and leave off the body entirely.

#addStaticDir should optionally accept a path under which to serve directory

If I have a directory of views called pages I should be able to specify something likeaddStaticDir("/pages", { serveFrom: "/" }) which would mean that the server acts as if everything in /pages was actually located at /. E.g. /pages/index.html would actually be served at /index.html (or even at just / in the case of index.html files).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.