-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(1.1c): Re-factor - Separate start-up logic and Fastify instantia…
…tion code
- Loading branch information
Showing
2 changed files
with
19 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} 🚀`)); |