-
Notifications
You must be signed in to change notification settings - Fork 12
/
app.js
41 lines (32 loc) · 910 Bytes
/
app.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
const Trek = require('../../lib')
async function launch() {
const app = new Trek()
app.paths.set('app', { single: true })
app.paths.set('app/plugins', { glob: 'app/plugins/index.js', single: true })
app.paths.set('app/controllers', { glob: 'app/controllers/*.js' })
await app.bootUp()
app.use(async ({ logger, rawReq, rawRes }, next) => {
logger.info(rawReq)
await next()
logger.info(rawRes)
})
app.use(async ({ cookies }, next) => {
cookies.set('name', 'trek')
await next()
})
app.use(ctx => {
if (ctx.req.path === '/') {
return ctx.res.send(200, 'Star Trek!')
} else if (ctx.req.path === '/error') {
throw new Error('Nothing')
}
// Something else return 404
ctx.cookies.set('name', null)
ctx.res.send(404)
})
app.on('error', err => {
app.logger.error(err)
})
await app.run(3000)
}
launch().catch(console.error)