/
index.js
31 lines (27 loc) · 1008 Bytes
/
index.js
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
const customRoutesMiddleware = require('./middlewares/custom-routes')
const jsonServer = require('json-server')
const server = jsonServer.create()
const router = jsonServer.router('db.json')
const middlewares = jsonServer.defaults()
const customMiddlewares = customRoutesMiddleware(router)
// Set default middlewares (logger, static, cors and no-cache)
server.use(middlewares)
server.use(jsonServer.bodyParser)
server.use((req, res, next) => {
console.log({
path: req.path,
body: req.body
})
next()
})
server.get('/customers/:uuid', customMiddlewares.customer)
server.post('/customers/:uuid/savings', customMiddlewares.createSavingsAccount)
server.get('/accounts', customMiddlewares.serverError)
server.get('/accounts/:id', customMiddlewares.account)
server.get('/transactions', customMiddlewares.notFound)
server.post('/accounts/:id/transfer', customMiddlewares.createTransfer)
// Use default router
server.use(router)
server.listen(3000, () => {
console.log('JSON Server is running')
})