-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
47 lines (41 loc) · 1 KB
/
server.ts
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
import Fastify from 'fastify'
import FastifyVite from 'fastify-vite'
import FastifyDXVue from 'fastify-dx-vue'
import FastifyView from '@fastify/view'
import FastifyStatic from '@fastify/static'
import nunjucks from 'nunjucks'
const server = Fastify()
server.decorate('db', {
todoList: [
'Do laundry',
'Respond to emails',
'Write report',
]
})
server.put('/api/todo/items', (req, reply) => {
server.db.todoList.push(req.body.item)
reply.send({ ok: true })
})
server.delete('/api/todo/items', (req, reply) => {
server.db.todoList.splice(req.body.index, 1)
reply.send({ ok: true })
})
server.register(FastifyStatic, {
root: '/static',
prefix: '/static/', // optional: default '/'
})
server.register(FastifyView, {
engine: {
nunjucks: nunjucks,
},
});
server.get("/", (req, reply) => {
// @ts-ignore
reply.view("./templates/index.njk")
});
await server.register(FastifyVite, {
root: import.meta.url,
renderer: FastifyDXVue,
})
await server.vite.ready()
await server.listen(3000)