Introduction
json-server gives front-end developers a fully functional REST API backed by a plain JSON file. It supports GET, POST, PUT, PATCH, DELETE, filtering, pagination, sorting, and full-text search out of the box, letting teams prototype and test without waiting for a real backend.
What json-server Does
- Generates RESTful routes from any JSON file automatically
- Supports filtering, pagination, sorting, and full-text search via query parameters
- Watches the JSON file for changes and reloads data live
- Serves static files alongside the API
- Supports custom routes, middlewares, and module-based usage in Node.js
Architecture Overview
json-server is built on Express.js and uses lowdb (a small local JSON database) to persist data. When you start the server, it reads your JSON file, creates RESTful endpoints for each top-level key, and writes mutations back to disk. The v1 branch simplified the codebase and added native TypeScript support.
Self-Hosting & Configuration
- Install globally via npm or use npx for one-off runs
- Structure your
db.jsonwith top-level keys as resource collections - Use
--portto change the default port from 3000 - Add a
routes.jsonfile to define custom URL rewrites - For programmatic use, import json-server as a module and attach custom middleware
Key Features
- Zero configuration required for basic REST operations
- Supports nested resources and relationship expansion via
_embedand_expand - Custom routes allow URL aliasing for realistic API simulation
- Middleware support for authentication stubs and logging
- Snapshot feature saves the current database state to a file
Comparison with Similar Tools
- Mirage JS — runs in the browser, intercepts fetch/XHR; json-server runs as a real HTTP server
- MSW (Mock Service Worker) — intercepts requests at the network level; json-server provides a standalone server process
- Prism (Stoplight) — generates mock servers from OpenAPI specs; json-server works from plain JSON
- WireMock — Java-based HTTP mock server for contract testing; heavier setup
- Mockoon — desktop app with GUI for mock APIs; json-server is CLI-first and scriptable
FAQ
Q: Can json-server handle large datasets? A: It works well for development and prototyping. For datasets beyond a few thousand records, consider a real database since lowdb reads the entire file into memory.
Q: Does json-server support authentication? A: Not natively, but you can add Express middleware to handle JWT or basic auth.
Q: Can I use json-server with TypeScript projects? A: Yes. Version 1.x ships with TypeScript types and can be imported as an ES module.
Q: Is data persistent across restarts? A: Yes. Changes are written back to the JSON file, so data survives server restarts.