bun-router 0.2.5 → 0.2.7
Sign up to get free protection for your applications and to get access to all the features.
- package/examples/space.ts +16 -0
- package/lib/router/router.ts +6 -5
- package/package.json +1 -1
- package/tests/router.test.ts +1 -1
@@ -0,0 +1,16 @@
|
|
1
|
+
import { router, html, json } from '..';
|
2
|
+
|
3
|
+
const r = router();
|
4
|
+
|
5
|
+
const aliens = '👽'.repeat(30);
|
6
|
+
|
7
|
+
r.add('/', 'GET', (req) => {
|
8
|
+
const message = {
|
9
|
+
page: 'home',
|
10
|
+
space: 'home',
|
11
|
+
body: 'ok'
|
12
|
+
};
|
13
|
+
return json(message);
|
14
|
+
});
|
15
|
+
|
16
|
+
r.serve();
|
package/lib/router/router.ts
CHANGED
@@ -35,10 +35,7 @@ const file = async (filepath: string): Promise<Response> => {
|
|
35
35
|
}
|
36
36
|
|
37
37
|
const html = async (content: string): Promise<Response> => {
|
38
|
-
const
|
39
|
-
const c = buff.toString('hex');
|
40
|
-
|
41
|
-
const response = new Response(c, {
|
38
|
+
const response = new Response(content, {
|
42
39
|
status: 200,
|
43
40
|
statusText: 'ok',
|
44
41
|
headers: {'Content-Type': 'text/html; charset=utf-8'},
|
@@ -75,7 +72,7 @@ const extractParams = (route: Route, req: HttpRequest) => {
|
|
75
72
|
}
|
76
73
|
|
77
74
|
const match = (route: Route, req: HttpRequest): boolean => {
|
78
|
-
return req.params.size !== 0 || route.
|
75
|
+
return req.params.size !== 0 || route.pattern === (new URL(req.request.url)).pathname
|
79
76
|
}
|
80
77
|
|
81
78
|
const router: Router = (port?: number | string, options?: Options) => {
|
@@ -104,6 +101,10 @@ const router: Router = (port?: number | string, options?: Options) => {
|
|
104
101
|
|
105
102
|
extractParams(route, httpRequest);
|
106
103
|
|
104
|
+
if (match(route, httpRequest))
|
105
|
+
return route.callback(httpRequest);
|
106
|
+
|
107
|
+
|
107
108
|
if (match(route, httpRequest)) return route.callback(httpRequest);
|
108
109
|
}
|
109
110
|
return new Response('not found');
|
package/package.json
CHANGED
package/tests/router.test.ts
CHANGED