A minimal Express-based REST API backed by a flat JSON data store. It ships with a curated data/db.json sample dataset and is preconfigured with both a custom Express server (src/server.js) and the json-server toolkit for rapid prototyping and manipulation of the data placed under data/.
- Express server with CRUD routes for posts (and simple readers for comments) wired to the shared JSON data file.
- Hot reloading via
nodemon --legacy-watchfor environments where file watching is limited. - JSON Server compatibility so you can spin up additional mock endpoints or explore the dataset with zero coding.
- Node.js 18 or newer (LTS recommended)
- npm 9+ (bundled with Node.js)
Verify your versions:
node --version
npm --version- Install dependencies:
npm install
- (Optional) Review the sample dataset in
data/db.jsonand tailor it to your needs. The structure includesposts,comments, anduserscollections by default.
The primary development server is defined in src/server.js. It exposes CRUD operations against the posts collection and read-only access for comments.
Start it with nodemon:
npm start- The server listens on
http://localhost:3000. - Requests are read/written directly against
data/db.json. - Newly created posts receive incremental numeric IDs.
| Method | Path | Description |
|---|---|---|
| GET | /posts |
List all posts |
| GET | /posts/:id |
Get a single post by ID |
| POST | /posts |
Create a new post (provide JSON body) |
| PUT | /posts/:id |
Update an existing post |
| DELETE | /posts/:id |
Remove a post |
| GET | /users/:id |
Get a single user by ID |
| GET | /comments |
List all comments |
# List posts
curl http://localhost:3000/posts
# Create a post
curl -X POST http://localhost:3000/posts \
-H 'Content-Type: application/json' \
-d '{"title":"New Article","userId":3}'
# Update a post
curl -X PUT http://localhost:3000/posts/1 \
-H 'Content-Type: application/json' \
-d '{"title":"Revised Title"}'
# Delete a post
curl -X DELETE http://localhost:3000/posts/3Changes made through the API immediately persist in data/db.json.
This project also bundles json-server for teams that prefer its full CRUD scaffolding against the same dataset.
Start JSON Server (optional) on a different port:
npx json-server --watch data/db.json --port 4000 --delay 200- The
--watchflag keeps the server in sync with edits todb.json. --port 4000avoids clashing with the Express server.--delay 200simulates network latency; adjust or remove as needed.
JSON Server automatically creates RESTful routes for every top-level key in db.json, giving you endpoints such as /posts, /comments, and /users with filtering, sorting, and pagination support out of the box. See the JSON Server documentation for advanced query patterns.
- Keep
data/db.jsonwell-formed JSON; syntax errors will surface in the server logs. - If you change the data structure, update the Express routes accordingly or rely on JSON Server for ultra-rapid prototyping.
- For production scenarios, consider replacing the flat-file persistence with a database driver and moving secrets into environment variables before deployment.
Happy prototyping!