JSON server (json-server)

Note: This post is a details-post for the post: Learning web app development through free online tutorials – Organized Notes and Log, https://raviswdev.blogspot.com/2024/03/learning-web-app-development-through.html .

How to Use JSON Server for Front-end Development, https://www.freecodecamp.org/news/json-server-for-frontend-development/

When I did npm install json-server, the latest version (I think it was 1.0.0-alpha.21) got installed. It did not support an option to print the request it gets. It listed very few options when I used npx json-server –help.

The Dave Gray tutorial json-server prints out the request it gets which is quite useful.

JSON Server (json-server), https://www.digitalocean.com/community/tutorials/json-server, published in Aug. 2022 shows json-server version 0.8.10 supporting much more options with a -q option to suppress log messages from output (so log messages printed seems to be the default for this version).

So I uninstalled the latest version json-server using ‘npm uninstall json-server’.

Then I installed version 0.8.10 using this command: ‘npm install json-server@0.8.10’. However running it gave some error. so I uninstalled it.
Then I tried ‘npm install json-server@0.17.4’ (first non-alpha version in https://www.npmjs.com/package/json-server?activeTab=versions). ‘npx json-server –help’ listed many options including -q.
‘npx json-server -p 3500 .\data\db.json’ ran the json-server and it reported the requests in the console e.g.:
POST /Tasks 500 308.811 ms – –
GET /Tasks 200 35.989 ms – –
….

Note: JSON server crashes after deletion of many tasks (sometimes as less as ten or twenty tasks) done through Clear All button (clearAllTasks() function). Restarting JSON server and refreshing my coding variation of Dave Gray tutorial React app (page) gets the app to working state again.
Later noted that at times clearAllTasks() results in JSON server reporting success in deletion of all tasks but the db.json file having few tasks (with an additional ~db.json file also being created and that having 0 tasks). Doing an additional db.json operation through json-server like adding one task, results in db.json file issue getting fixed (json-server removes those remaining tasks and only the added task is shown in the db.json file).

Comments

Archive