@@ -8,52 +8,141 @@ Router for Node.js, micro and others
88This router is intended to be used with native node http interface. Features:
99- Written in TypeScript with focus on type safety.
1010- Extensible via [ ` Matcher ` ] ( src/matchers/Matcher.ts ) and [ ` MatchResult ` ] ( src/matchers/MatchResult.ts ) interfaces.
11- - Works with [ native node http server] ( https://nodejs.org/api/http.html#http_class_http_server ) ( [ example ] ( src/examples/ node.ts ) ).
12- - Works with [ micro] ( https://github.com/zeit/ micro) ( [ example ] ( src/examples/micro.ts ) ).
11+ - Works with [ native node http server] ( #usage-with-native- node-http-server ) .
12+ - Works with [ micro] ( #usage-with- micro) .
1313- Offers a set of matchers:
14+ - [ ` MethodMatcher ` ] ( #methodmatcher )
15+ - [ ` ExactUrlPathnameMatcher ` ] ( #exacturlpathnamematcher )
16+ - [ ` ExactQueryMatcher ` ] ( #exactquerymatcher )
17+ - Powerful [ ` RegExpUrlMatcher ` ] ( #regexpurlmatcher )
18+ - Convenient [ ` EndpointMatcher ` ] ( #endpointmatcher )
1419 - ` AndMatcher ` and ` OrMatcher `
15- - ` ExactQueryMatcher `
16- - Convenient ` EndpointMatcher `
17- - ` ExactUrlPathnameMatcher `
18- - ` MethodMatcher `
19- - Powerful ` RegExpUrlMatcher `
2020- Can be used with [ path-to-regexp] ( https://github.com/pillarjs/path-to-regexp ) .
2121- Work with another servers? Tell it me!
2222
23- First example with micro
24- ------------------------
23+ ## Installation
24+
25+ Choose for one of your favourite package manager:
26+
27+ ``` bash
28+ npm install @bessonovs/node-http-router
29+ yarn add @bessonovs/node-http-router
30+ pnpm add @bessonovs/node-http-router
31+ ```
32+
33+ ## Documentation and examples
34+
35+ ### Usage with native node http server
36+
37+ ``` typescript
38+ const router = new Router ((req , res ) => {
39+ res .statusCode = 404
40+ res .end ()
41+ })
42+
43+ const server = http .createServer (router .serve ).listen (8080 , ' localhost' )
44+
45+ router .addRoute ({
46+ matcher: new ExactUrlPathnameMatcher ([' /hello' ]),
47+ handler : () => ' Hello kitty!' ,
48+ })
49+ ```
50+
51+ See [ full example] ( src/examples/node.ts ) and [ native node http server] ( https://nodejs.org/api/http.html#http_class_http_server ) documentation.
52+
53+ ### Usage with micro
54+
55+ [ micro] ( https://github.com/vercel/micro ) is a very lightweight layer around the native node http server with some convenience methods.
56+
2557``` typescript
2658// specify default handler
27- const router = new Router ((req , res ) => sendError (req , res , { statusCode: 404 }))
59+ const router = new Router ((req , res ) => send (res , 404 ))
60+
61+ http .createServer (micro (router .serve )).listen (8080 , ' localhost' )
2862
2963router .addRoute ({
3064 matcher: new ExactUrlPathnameMatcher ([' /hello' ]),
3165 handler : () => ' Hello kitty!' ,
3266})
67+ ```
68+
69+ See [ full example] ( src/examples/micro.ts ) .
3370
71+ ### MethodMatcher
3472
35- const [address, port] = [' localhost' , 8080 ]
36- http .createServer (micro (router .serve )).listen (port , address )
73+ Method matcher is the simplest matcher and matches any of the passed http methods:
74+
75+ ``` typescript
76+ router .addRoute ({
77+ matcher: new MethodMatcher ([' OPTIONS' , ' POST' ]),
78+ // method is either OPTIONS or POST
79+ handler : (req , res , { method }) => ` Method: ${method } ` ,
80+ })
3781```
3882
83+ ### ExactUrlPathnameMatcher
3984
40- Installation
41- ------------
85+ Matches given pathnames (but ignores query parameters):
4286
43- Choose for one of your favourite package manager:
87+ ``` typescript
88+ router .addRoute ({
89+ matcher: new ExactUrlPathnameMatcher ([' /v1/graphql' , ' /v2/graphql' ]),
90+ // pathname is /v1/graphql or /v2/graphql
91+ handler : (req , res , { pathname }) => ` Path is ${pathname } ` ,
92+ })
93+ ```
4494
45- ``` bash
46- pnpm install @bessonovs/node-http-router
47- npm install @bessonovs/node-http-router
48- yarn add @bessonovs/node-http-router
95+ ### ExactQueryMatcher
96+
97+ Defines expectations on query parameters:
98+
99+ ``` typescript
100+ router .addRoute ({
101+ matcher: new ExactQueryMatcher ({
102+ // example of 4 query parameters:
103+ // true defines mandatory parameters
104+ mustPresent: true ,
105+ // false defines parameters expected to absent
106+ mustAbsent: false ,
107+ // undefined defines optional parameters. They
108+ // aren't used for matching, but available as type
109+ isOptional: undefined ,
110+ // a string defines expected parameter name and value
111+ mustExact: ' exactValue' ,
112+ }),
113+ // query parameter isOptional has type string | undefined
114+ handler : (req , res , { query }) => query .isOptional ,
115+ })
116+ ```
117+
118+ ### RegExpUrlMatcher
119+
120+ Allows powerful expressions:
121+
122+ ``` typescript
123+ router .addRoute ({
124+ matcher: new RegExpUrlMatcher <{ userId: string }>([/ ^ \/ group\/ (?<userId >[^ /] + )$ / ]),
125+ handler : (req , res , { match }) => ` User id is: ${match .groups .userId } ` ,
126+ })
127+ ```
128+ Ordinal parameters can be used too. Be aware that regular expression must match the whole base url (also with query parameters) and not only ` pathname ` .
129+
130+ ### EndpointMatcher
131+
132+ EndpointMatcher is a combination of Method and RegExpUrl matcher for convenient usage:
133+
134+ ``` typescript
135+ router .addRoute ({
136+ matcher: new EndpointMatcher <{ userId: string }>(' GET' , / ^ \/ group\/ (?<userId >[^ /] + )$ / ),
137+ handler : (req , res , { match }) => ` Group id is: ${match .groups .userId } ` ,
138+ })
49139```
50140
51- License
52- -------
141+ ## License
53142
54143The MIT License (MIT)
55144
56- Copyright (c) 2019, Anton Bessonov
145+ Copyright (c) 2019 - today , Anton Bessonov
57146
58147Permission is hereby granted, free of charge, to any person obtaining a copy
59148of this software and associated documentation files (the "Software"), to deal
0 commit comments