-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
51 lines (41 loc) · 1 KB
/
index.js
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
41
42
43
44
45
46
47
48
49
50
51
const { Engine: Trek, Router } = require('../../lib')
async function launch() {
const app = new Trek()
const router = new Router()
router.add('GET', '/', async ({ res }) => {
res.send(200, 'Hello, Trek!')
})
router.add('GET', '/startrek', async ({ res }) => {
res.type = 'html'
res.send(200, Buffer.from('Hello, Star Trek!'))
})
router.add('POST', '/', async ({ res }) => {
res.send(200, {
status: 'ok',
message: 'success'
})
})
app.use(async ({ req, res }, next) => {
const start = new Date()
await next()
const ms = new Date() - start
console.log(`${ms}ms`)
})
app.use(async ({ req, res }, next) => {
const route = router.find(req.method, req.path)
if (route) {
const [handler] = route
if (handler !== undefined) {
await handler({ req, res })
return
}
}
await next()
})
app.use(async ({ res }) => {
res.status = 404
res.end()
})
await app.run(3000)
}
launch().catch(console.error)