UNPKG

3.57 kBMarkdownView Raw
1# http-hash-router
2
3Server route handler for http-hash
4
5## Example
6
7```js
8var http = require('http');
9var HttpHashRouter = require('http-hash-router');
10
11var router = HttpHashRouter();
12
13router.set('/health', function health(req, res) {
14 res.end('OK');
15});
16
17var 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});
28server.listen(3000);
29```
30
31## Documentation
32
33### `var router = HttpHashRouter()`
34
35```ocaml
36type NotFoundError : Error & {
37 type: "http-hash-router.not-found",
38 statusCode: 404
39}
40
41type 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
50http-hash-router : () => Router
51```
52
53`HttpHashRouter` will create a new router function.
54
55The `HttpHashRouter` itself takes no options and returns a
56function that takes four arguments, `req`, `res`, `opts`, `cb`.
57
58### `router(req, res, opts, cb)`
59
60```ocaml
61type NotFoundError : Error & {
62 type: "http-hash-router.not-found",
63 statusCode: 404
64}
65
66router : (
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
76It is expected that you call the `router` function with the
77`HTTPRequest` and `HTTPResponse` as the first and second
78arguments.
79
80The third argument is the options object. The `router` will
81copy the options object and set the `params` and `splat` field.
82
83The fourth argument is a callback function, this function
84either gets called with a `http-hash-router.not-found` error
85or gets passed to the route handler function.
86
87If you do not pass a callback to the `router` function then
88it will throw the `http-hash-router.expected-callback` exception.
89
90### `router.set(pattern, handler)`
91
92```ocaml
93type RoutePattern : String
94type 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
104set : (RoutePattern, RouteHandler) => void
105```
106
107You can call `.set()` on the router and it will internally
108store 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
115The `.set()` functionality is implemented by
116[`http-hash`][http-hash] itself and you can find documentation
117for it at [HttpHash#set][http-hash-set].
118
119Your 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
128If your route pattern contains a param, i.e. `"/foo/:bar"` or
129your route pattern contains a splat, i.e. `"/foo/*"` then
130the 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