1 | # http-hash-router
|
2 |
|
3 | Server route handler for http-hash
|
4 |
|
5 | ## Example
|
6 |
|
7 | ```js
|
8 | var http = require('http');
|
9 | var HttpHashRouter = require('http-hash-router');
|
10 |
|
11 | var router = HttpHashRouter();
|
12 |
|
13 | router.set('/health', function health(req, res) {
|
14 | res.end('OK');
|
15 | });
|
16 |
|
17 | var server = http.createServer(function handler(req, res) {
|
18 | router(req, res, {}, onError);
|
19 |
|
20 | function onError(err) {
|
21 | if (err) {
|
22 | // use your own custom error serialization.
|
23 | res.statusCode = err.statusCode || 500;
|
24 | res.end(err.message);
|
25 | }
|
26 | }
|
27 | });
|
28 | server.listen(3000);
|
29 | ```
|
30 |
|
31 | ## Documentation
|
32 |
|
33 | ### `var router = HttpHashRouter()`
|
34 |
|
35 | ```ocaml
|
36 | type NotFoundError : Error & {
|
37 | type: "http-hash-router.not-found",
|
38 | statusCode: 404
|
39 | }
|
40 |
|
41 | type Router : {
|
42 | set: (pattern: String, handler: Function | Object) => void
|
43 | } & (
|
44 | req: HttpReqest,
|
45 | res: HttpResponse,
|
46 | opts: Object,
|
47 | cb: Callback<NotFoundError | Error, void>
|
48 | ) => void
|
49 |
|
50 | http-hash-router : () => Router
|
51 | ```
|
52 |
|
53 | `HttpHashRouter` will create a new router function.
|
54 |
|
55 | The `HttpHashRouter` itself takes no options and returns a
|
56 | function that takes four arguments, `req`, `res`, `opts`, `cb`.
|
57 |
|
58 | ### `router(req, res, opts, cb)`
|
59 |
|
60 | ```ocaml
|
61 | type NotFoundError : Error & {
|
62 | type: "http-hash-router.not-found",
|
63 | statusCode: 404
|
64 | }
|
65 |
|
66 | router : (
|
67 | req: HttpReqest,
|
68 | res: HttpResponse,
|
69 | opts: Object,
|
70 | cb: Callback<NotFoundError | Error, void>
|
71 | ) => void
|
72 | ```
|
73 |
|
74 | - throw `http-hash-router.expected.callback` exception.
|
75 |
|
76 | It is expected that you call the `router` function with the
|
77 | `HTTPRequest` and `HTTPResponse` as the first and second
|
78 | arguments.
|
79 |
|
80 | The third argument is the options object. The `router` will
|
81 | copy the options object and set the `params` and `splat` field.
|
82 |
|
83 | The fourth argument is a callback function, this function
|
84 | either gets called with a `http-hash-router.not-found` error
|
85 | or gets passed to the route handler function.
|
86 |
|
87 | If you do not pass a callback to the `router` function then
|
88 | it will throw the `http-hash-router.expected-callback` exception.
|
89 |
|
90 | ### `router.set(pattern, handler)`
|
91 |
|
92 | ```ocaml
|
93 | type RoutePattern : String
|
94 | type RouteHandler : Object<method: String, RouteHandler> | (
|
95 | req: HttpRequest,
|
96 | res: HttpResponse,
|
97 | opts: Object & {
|
98 | params: Object<String, String>,
|
99 | splat: String | null
|
100 | },
|
101 | cb: Callback<Error, void>
|
102 | ) => void
|
103 |
|
104 | set : (RoutePattern, RouteHandler) => void
|
105 | ```
|
106 |
|
107 | You can call `.set()` on the router and it will internally
|
108 | store your handler against the pattern.
|
109 |
|
110 | `.set()` takes a route pattern and a route handler. A route
|
111 | handler is either a function or an object. If you use
|
112 | an object then we will create a route handler function
|
113 | using the [`http-methods`][http-methods] module.
|
114 |
|
115 | The `.set()` functionality is implemented by
|
116 | [`http-hash`][http-hash] itself and you can find documentation
|
117 | for it at [HttpHash#set][http-hash-set].
|
118 |
|
119 | Your handler function will get called with four arguments.
|
120 |
|
121 | - `req` the http request stream
|
122 | - `res` the http response stream
|
123 | - `opts` options object. This contains properties defined
|
124 | in the server and also contains the `params` and `splat`
|
125 | fields.
|
126 | - `cb` callback.
|
127 |
|
128 | If your route pattern contains a param, i.e. `"/foo/:bar"` or
|
129 | your route pattern contains a splat, i.e. `"/foo/*"` then
|
130 | the values of the params and splat will be passed to the
|
131 | `params` and `splat` field on `opts`.
|
132 |
|
133 | [http-hash]: https://github.com/Matt-Esch/http-hash
|
134 | [http-hash-set]: https://github.com/Matt-Esch/http-hash#hashsetpath-handler
|
135 | [http-methods]: https://github.com/Raynos/http-methods
|