Skip to content

Commit

Permalink
feat(1.1c): Re-factor - Separate start-up logic and Fastify instantia…
Browse files Browse the repository at this point in the history
…tion code
  • Loading branch information
one-aalam committed Jul 12, 2021
1 parent 07e16e8 commit 7025757
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
17 changes: 17 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Server-app, not the Server!
import Fastify, { FastifyInstance } from 'fastify'
import { USERS, RECIPES, RECIPE_CATEGORIES } from './fixtures'

export const buildSever = (): FastifyInstance => {
// Instantiate the Fastify server
const fastify = Fastify()

// Register handlers to routes
fastify.get('/users', async () => USERS)
fastify.get('/recipes', async () => RECIPES)
fastify.get('/categories', async () => RECIPE_CATEGORIES)
fastify.get('*', async (req) => `You requested for ${req.url} using method ${req.method}, which does not have an associated response`)

// return the Server instance
return fastify
}
13 changes: 2 additions & 11 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import Fastify from 'fastify'
import { USERS, RECIPES, RECIPE_CATEGORIES } from './fixtures'
import { buildSever } from './app'

const APP_PORT = process.env.APP_PORT || 3000

const server = Fastify()

server.get('/users', async () => USERS)

server.get('/recipes', async () => RECIPES)

server.get('/categories', async () => RECIPE_CATEGORIES)

server.get('*', async (req) => `You requested for ${req.url} using method ${req.method}, which does not have an associated response`)
const server = buildSever()

server.listen(APP_PORT, () => console.log(`Server started on ${APP_PORT} 🚀`));

0 comments on commit 7025757

Please sign in to comment.