Skip to content

Commit f45095b

Browse files
committed
update docs
1 parent 2fd9a55 commit f45095b

File tree

3 files changed

+116
-26
lines changed

3 files changed

+116
-26
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019 Anton Bessonov
3+
Copyright (c) 2019 - today Anton Bessonov
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 111 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,52 +8,141 @@ Router for Node.js, micro and others
88
This 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

2963
router.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

54143
The MIT License (MIT)
55144

56-
Copyright (c) 2019, Anton Bessonov
145+
Copyright (c) 2019 - today, Anton Bessonov
57146

58147
Permission is hereby granted, free of charge, to any person obtaining a copy
59148
of this software and associated documentation files (the "Software"), to deal

src/examples/micro.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import http from 'http'
2-
import micro, { sendError } from 'micro'
2+
import micro, { send } from 'micro'
33
import { Router } from '../router'
44
import {
5-
EndpointMatcher, ExactUrlPathnameMatcher,
5+
EndpointMatcher,
6+
ExactUrlPathnameMatcher,
67
} from '../matchers'
78

89
/*
@@ -17,7 +18,7 @@ yarn example-micro-start
1718
1819
*/
1920

20-
const router = new Router((req, res) => sendError(req, res, { statusCode: 404 }))
21+
const router = new Router((req, res) => send(res, 404))
2122

2223
const [address, port] = ['localhost', 8080]
2324

0 commit comments

Comments
 (0)