Code Monkey home page Code Monkey logo

http-hash-router's Introduction

http-hash-router

Server route handler for http-hash

Example

var http = require('http');
var HttpHashRouter = require('http-hash-router');

var router = HttpHashRouter();

router.set('/health', function health(req, res) {
    res.end('OK');
});

var server = http.createServer(function handler(req, res) {
    router(req, res, {}, onError);

    function onError(err) {
        if (err) {
            // use your own custom error serialization.
            res.statusCode = err.statusCode || 500;
            res.end(err.message);
        }
    }
});
server.listen(3000);

Documentation

var router = HttpHashRouter()

type NotFoundError : Error & {
    type: "http-hash-router.not-found",
    statusCode: 404
}

type Router : {
    set: (pattern: String, handler: Function | Object) => void
} & (
    req: HttpReqest,
    res: HttpResponse,
    opts: Object,
    cb: Callback<NotFoundError | Error, void>
) => void

http-hash-router : () => Router

HttpHashRouter will create a new router function.

The HttpHashRouter itself takes no options and returns a function that takes four arguments, req, res, opts, cb.

router(req, res, opts, cb)

type NotFoundError : Error & {
    type: "http-hash-router.not-found",
    statusCode: 404
}

router : (
    req: HttpReqest,
    res: HttpResponse,
    opts: Object,
    cb: Callback<NotFoundError | Error, void>
) => void
  • throw http-hash-router.expected.callback exception.

It is expected that you call the router function with the HTTPRequest and HTTPResponse as the first and second arguments.

The third argument is the options object. The router will copy the options object and set the params and splat field.

The fourth argument is a callback function, this function either gets called with a http-hash-router.not-found error or gets passed to the route handler function.

If you do not pass a callback to the router function then it will throw the http-hash-router.expected-callback exception.

router.set(pattern, handler)

type RoutePattern : String
type RouteHandler : Object<method: String, RouteHandler> | (
    req: HttpRequest,
    res: HttpResponse,
    opts: Object & {
        params: Object<String, String>,
        splat: String | null
    },
    cb: Callback<Error, void>
) => void

set : (RoutePattern, RouteHandler) => void

You can call .set() on the router and it will internally store your handler against the pattern.

.set() takes a route pattern and a route handler. A route handler is either a function or an object. If you use an object then we will create a route handler function using the http-methods module.

The .set() functionality is implemented by http-hash itself and you can find documentation for it at HttpHash#set.

Your handler function will get called with four arguments.

  • req the http request stream
  • res the http response stream
  • opts options object. This contains properties defined in the server and also contains the params and splat fields.
  • cb callback.

If your route pattern contains a param, i.e. "/foo/:bar" or your route pattern contains a splat, i.e. "/foo/*" then the values of the params and splat will be passed to the params and splat field on opts.

http-hash-router's People

Contributors

matt-esch avatar raynos avatar uberesch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

http-hash-router's Issues

Make router and handler opts optional

What are your thoughts on making opts for route handlers optional? Would you accept a PR for that?

E.G

router(req, res, [opts], cb)

and

handler(req, res, [opts], cb)

That way connect middleware function signatures might have a chance to work without any additional closures.

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.