server link - https://important-hen-miniskirt.cyclic.app/
- Create a folder
mkdir mock_server
cd mock_server
npm init -y or npm init
npm install json-server
const jsonServer = require('json-server')
const server = jsonServer.create()
const router = jsonServer.router('db.json')
const middlewares = jsonServer.defaults()
const PORT = process.env.PORT || 8080 (any port number you want to use)
server.use(middlewares)
server.use(router)
server.listen(PORT, () => {
console.log(`listening on http://localhost:${PORT}`)
})
{
"posts": [
{ "id": 1, "title": "json-server", "author": "typicode" }
],
"comments": [
{ "id": 1, "body": "some comment", "postId": 1 }
],
"profile": { "name": "typicode" }
}
"start": "node index.js",
npm run start - it will start the server.
http://localhost:8080/posts
Output:
[
{ "id": 1, "title": "json-server", "author": "typicode" }
]