Skip to content

Commit

Permalink
feat(1.1c): Add + Integrate Fastify
Browse files Browse the repository at this point in the history
  • Loading branch information
one-aalam committed Jul 12, 2021
1 parent 5c6d6a2 commit 7abbc25
Show file tree
Hide file tree
Showing 3 changed files with 266 additions and 22 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"typescript": "^4.3.5"
},
"dependencies": {
"dotenv": "^10.0.0"
"dotenv": "^10.0.0",
"fastify": "^3.19.0"
},
"husky": {
"hooks": {
Expand Down
32 changes: 17 additions & 15 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import http from 'http'
import Fastify from 'fastify'
import { USERS, RECIPES, RECIPE_CATEGORIES } from './fixtures'

const APP_PORT = process.env.APP_PORT || 3000

const server = http.createServer((req: http.IncomingMessage, res: http.OutgoingMessage) => {
const route = req.method + ' ' + req.url // ex: GET /users
const server = Fastify()

if (route === 'GET /users') {
res.end(JSON.stringify(USERS))
} else if (route === 'GET /recipes') {
res.end(JSON.stringify(RECIPES))
} else if (route === 'GET /categories') {
res.end(JSON.stringify(RECIPE_CATEGORIES))
} else {
res.end(
`You requested for ${req.url} using method ${req.method}, which does not have an associated response`
)
}
server.get('/users', (req, res) => {
res.send(USERS)
})

server.listen(APP_PORT, () => console.log(`Server started on ${APP_PORT} 🚀`))
server.get('/recipes', (req, res) => {
res.send(RECIPES)
})

server.get('/categories', (req, res) => {
res.send(RECIPE_CATEGORIES)
})

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

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

0 comments on commit 7abbc25

Please sign in to comment.