What is json-server?
json-server is a full fake REST API with zero coding in less than 30 seconds. It allows you to create a mock REST API using a simple JSON file as the database.
What are json-server's main functionalities?
Create a Fake REST API
This code sets up a basic JSON server that reads from a 'db.json' file and serves it as a REST API. The server listens on port 3000.
const jsonServer = require('json-server');
const server = jsonServer.create();
const router = jsonServer.router('db.json');
const middlewares = jsonServer.defaults();
server.use(middlewares);
server.use(router);
server.listen(3000, () => {
console.log('JSON Server is running');
});
Custom Routes
This code demonstrates how to use custom routes with json-server. The rewriter middleware is used to map custom routes to the default routes.
const jsonServer = require('json-server');
const server = jsonServer.create();
const router = jsonServer.router('db.json');
const middlewares = jsonServer.defaults();
server.use(middlewares);
server.use(jsonServer.rewriter({
'/api/*': '/$1',
'/blog/:resource/:id/show': '/:resource/:id'
}));
server.use(router);
server.listen(3000, () => {
console.log('JSON Server is running');
});
Add Custom Middleware
This code adds custom middleware to the JSON server. In this example, a timestamp is added to the request body for POST requests.
const jsonServer = require('json-server');
const server = jsonServer.create();
const router = jsonServer.router('db.json');
const middlewares = jsonServer.defaults();
server.use(middlewares);
server.use((req, res, next) => {
if (req.method === 'POST') {
req.body.createdAt = Date.now();
}
next();
});
server.use(router);
server.listen(3000, () => {
console.log('JSON Server is running');
});
Other packages similar to json-server
miragejs
Mirage JS is a client-side server to build, test and share a complete working JavaScript application without having to rely on any backend services. Unlike json-server, which is more focused on quickly setting up a REST API, Mirage JS is designed to work seamlessly with frontend frameworks like React, Vue, and Angular.
prism
Prism is a package for creating mock servers based on OpenAPI and API Blueprint specifications. It allows you to create a mock server that can validate requests and responses against your API specification. Unlike json-server, which uses a JSON file as a database, Prism uses API specifications to generate mock responses.
json-server
Usage
Install json-server
npm install json-server
Create a db.json
file or run json-server db.json
to create one with some default resources
{
"posts": [
{ "id": "1", "title": "string" },
{ "id": "2", "title": "some post" }
],
"comments": [
{ "id": "1", "text": "some text", "postId": "1" },
{ "id": "2", "text": "some text", "postId": "1" }
]
}
json-server db.json
Run json-server --help
for a list of options
Routes
GET /posts
GET /posts/:id
POST /posts
PUT /posts
DELETE /posts/:id
Params
Comparison
→==
lt
→ <
lte
→ <=
gt
→ >
gte
→ >=
ne
→ !=
GET /posts?views_gt=9000
Range
GET /posts?_start=10&_end=20
GET /posts?_start=10&_limit=10
Paginate
page
per_page
(default = 10)
GET /posts?_page=1&_per_page=25
Sort
GET /posts?_sort=id,-views
Nested fields
Include
GET /posts?_include=comments
GET /comments?_include=post
Delete
DELETE /posts/1
DELETE /posts/1?_include=comments
Serving static files
If you create a ./public
directory, JSON Serve will serve its content in addition to the REST API. You can add custom directories using -s/--static
option.
json-server -s ./static
json-server -s ./static -s ./node_modules