Skip to content

Commit

Permalink
🔥 feat : deployment mock server
Browse files Browse the repository at this point in the history
  • Loading branch information
madhuredra committed Jan 16, 2024
1 parent 03953eb commit 5431ef8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 51 deletions.
33 changes: 23 additions & 10 deletions deployments/mock-server/api/server.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
// See https://github.com/typicode/json-server#module
const jsonServer = require('json-server')
const cors = require('cors')
const path = require('path')

const server = jsonServer.create()
const router = jsonServer.router(path.join(__dirname, 'db.json'))

// Uncomment to allow write operations
// const fs = require('fs')
// const path = require('path')
// const filePath = path.join('db.json')
// const data = fs.readFileSync(filePath, "utf-8");
// const db = JSON.parse(data);
// const router = jsonServer.router(db)

// Comment out to allow write operations
const router = jsonServer.router('db.json')

const middlewares = jsonServer.defaults()

server.use(cors())
server.use(jsonServer.bodyParser)
server.use(middlewares)
// Add this before server.use(router)
server.use(jsonServer.rewriter({
'/api/*': '/$1',
'/blog/:resource/:id/show': '/:resource/:id'
}))
server.use(router)
server.listen(3000, () => {
console.log('JSON Server is running')
})

const PORT = 8000

server.listen(PORT, () => {
console.log(`JSON Server is running on http://localhost:${PORT}`)
})
// Export the Server API
module.exports = server
34 changes: 1 addition & 33 deletions deployments/mock-server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions deployments/mock-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
"name": "json-server-vercel",
"version": "1.0.0",
"description": "Deploy JSON Server to Vercel",
"main": "server.js",
"main": "api/server.js",
"scripts": {
"start": "node server.js"
"start": "node api/server.js"
},
"dependencies": {
"cors": "^2.8.5",
"json-server": "^0.17.4",
"path": "^0.12.7"
"json-server": "^0.17.4"
}
}
}
2 changes: 1 addition & 1 deletion deployments/mock-server/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion deployments/mock-server/vercel.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
"destination": "api/server.js"
}
]
}
}

0 comments on commit 5431ef8

Please sign in to comment.