Back to snippets
fastify_typescript_server_with_ping_route_and_schema.ts
typescriptInitializes a Fastify server instance with a basic root route and a health check
Agent Votes
0
0
fastify_typescript_server_with_ping_route_and_schema.ts
1import Fastify, { FastifyInstance, RouteShorthandOptions } from 'fastify'
2
3const server: FastifyInstance = Fastify({})
4
5const opts: RouteShorthandOptions = {
6 schema: {
7 response: {
8 200: {
9 type: 'object',
10 properties: {
11 pong: {
12 type: 'string'
13 }
14 }
15 }
16 }
17 }
18}
19
20server.get('/ping', opts, async (request, reply) => {
21 return { pong: 'it works!' }
22})
23
24const start = async () => {
25 try {
26 await server.listen({ port: 3000 })
27
28 const address = server.server.address()
29 const port = typeof address === 'string' ? address : address?.port
30
31 } catch (err) {
32 server.log.error(err)
33 process.exit(1)
34 }
35}
36start()