-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
44 lines (38 loc) · 1.12 KB
/
index.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
import Fastify, { FastifyInstance, RouteShorthandOptions } from 'fastify'
import { bookRoutes, route } from './src/books/books.controller'
const server: FastifyInstance = Fastify({})
const opts: RouteShorthandOptions = {
schema: {
response: {
200: {
type: 'object',
properties: {
pong: {
type: 'string'
}
}
}
}
}
}
server.get('/ping', opts, async (request, reply) => {
return { pong: 'it worked!' }
})
server.register(function (app, _, done) {
app.get('/users', () => { return { users: 'users!' } })
app.route(route)
bookRoutes.forEach(route => app.route(route))
done()
}, { prefix: '/v1' }) // global route prefix
const start = async () => {
try {
await server.listen({ port: 3000 })
const address = server.server.address()
const port = typeof address === 'string' ? address : address?.port
console.log(`listening on port ${port}`)
} catch (err) {
server.log.error(err)
process.exit(1)
}
}
start()