-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
40 lines (24 loc) · 916 Bytes
/
server.js
File metadata and controls
40 lines (24 loc) · 916 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import http from 'node:http'
import {json} from "./middlewares/json.js";
import {routes} from "./routes.js";
import {extractQueryParams} from "./utils/extract-query-params.js";
const server = http.createServer(async (req, res) => {
const {method, url} = req
await json(req, res)
const route = routes.find(route => {
return route.method === method && route.path.test(url)
})
if (route) {
const routeParams = req.url.match(route.path)
const {query, ...params} = routeParams.groups
req.params = params
req.query = query ? extractQueryParams(query) : {}
req.params = {...routeParams.groups}
return route.handler(req, res)
}
console.log(route)
return res.writeHead(404).end('Not Found')
})
server.listen(3333, function(){
console.log('Server is listening on port 3333');
});