You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Can be used with [path-to-regexp](https://github.com/pillarjs/path-to-regexp).
21
-
- Work with another servers? Tell it me!
21
+
- Work with other servers? Tell it me!
22
+
23
+
From 2.0.0 the router isn't tied to node or even http anymore! Although the primary use case is still node's request routing, you can use it for use cases like event processing.
24
+
25
+
## Sponsoring
26
+
27
+
Contact me if you want to become a sponsor or need paid support.
See [releases](https://github.com/Bessonov/node-http-router/releases).
47
+
33
48
## Documentation and examples
34
49
35
50
### Binding
36
51
37
-
The router works with native http interfaces like `IncomingMessage` and `ServerResponse`. Therefore it should be possible to use it with most of existing servers.
52
+
The router doesn't depends on the native http interfaces like `IncomingMessage` and `ServerResponse`. Therefore, you can use it for everything. Below are some use cases.
38
53
39
54
#### Usage with native node http server
40
55
41
56
```typescript
42
-
const router =newRouter((req, res) => {
43
-
res.statusCode=404
44
-
res.end()
45
-
})
57
+
const router =newNodeHttpRouter()
46
58
47
59
const server =http.createServer(router.serve).listen(8080, 'localhost')
48
60
49
61
router.addRoute({
50
62
matcher: newExactUrlPathnameMatcher(['/hello']),
51
63
handler: () =>'Hello kitty!',
52
64
})
65
+
66
+
// 404 handler
67
+
router.addRoute({
68
+
matcher: newBooleanMatcher(true),
69
+
handler: ({ data: { res } }) =>send(res, 404)
70
+
})
53
71
```
54
72
55
73
See [full example](src/examples/node.ts) and [native node http server](https://nodejs.org/api/http.html#http_class_http_server) documentation.
56
74
57
-
#### Usage with micro
75
+
#### Usage with micro server
58
76
59
77
[micro](https://github.com/vercel/micro) is a very lightweight layer around the native node http server with some convenience methods.
#### Usage for event processing or generic use case
99
+
100
+
```typescript
101
+
// Custom type
102
+
typeMyEvent= {
103
+
name:'test1',
104
+
} | {
105
+
name:'test2',
106
+
} | {
107
+
name:'invalid',
108
+
}
109
+
110
+
const eventRouter =newRouter<MyEvent>()
111
+
112
+
eventRouter.addRoute({
113
+
// define matchers for event processing
114
+
matcher: ({
115
+
match(params:MyEvent):MatchResult<number> {
116
+
const result =/^test(?<num>\d+)$/.exec(params.name)
117
+
if (result?.groups?.num) {
118
+
return {
119
+
matched: true,
120
+
result: parseInt(result.groups.num)
121
+
}
122
+
}
123
+
return {
124
+
matched: false,
125
+
}
126
+
},
127
+
}),
128
+
// define event handler for matched events
129
+
handler({ data, match: { result } }) {
130
+
return`the event ${data.name} has number ${result}`
131
+
}
132
+
})
133
+
134
+
// add default handler
135
+
eventRouter.addRoute({
136
+
matcher: newBooleanMatcher(true),
137
+
handler({ data }) {
138
+
return`the event '${data.name}' is unknown`
139
+
}
140
+
})
141
+
142
+
// execute and get processing result
143
+
const result =eventRouter.exec({
144
+
name: 'test1',
145
+
})
146
+
```
147
+
75
148
### Matchers
76
149
77
150
In the core, matchers are responsible to decide if particular handler should be called or not. There is no magic: matchers are iterated on every request and first positive "match" calls defined handler.
@@ -84,7 +157,7 @@ Method matcher is the simplest matcher and matches any of the passed http method
handler: (req, res, { match }) =>`User id is: ${match.groups.userId}`,
206
+
handler: ({ match: { result: { match } } }) =>`User id is: ${match.groups.userId}`,
134
207
})
135
208
```
136
-
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`.
209
+
Be aware that regular expression must match the whole base url (also with query parameters) and not only `pathname`. Ordinal parameters can be used too.
handler: (req, res, { match }) =>`Group id is: ${match.groups.userId}`,
218
+
handler: ({ match: { result: { method, match } } }) =>`Group id ${match.groups.userId} matched with ${method} method`,
146
219
})
147
220
```
148
221
149
222
### Middlewares
150
223
151
-
**This section is highly experimental!**
224
+
**This whole section is highly experimental!**
152
225
153
226
Currently, there is no built-in API for middlewares. It seems like there is no aproach to provide centralized and typesafe way for middlewares. And it need some conceptual work, before it will be added. Open an issue, if you have a great idea!
154
227
@@ -157,8 +230,17 @@ Currently, there is no built-in API for middlewares. It seems like there is no a
0 commit comments