Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
json-server
Advanced tools
[![Node.js CI](https://github.com/typicode/json-server/actions/workflows/node.js.yml/badge.svg)](https://github.com/typicode/json-server/actions/workflows/node.js.yml)
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.
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');
});
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 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.
[!IMPORTANT] Viewing beta v1 documentation – usable but expect breaking changes. For stable version, see here
👋 Hey! Using React, Vue or Astro? Check my new project MistCSS to write 50% less code.
npm install json-server
Create a db.json
or db.json5
file
{
"posts": [
{ "id": "1", "title": "a title", "views": 100 },
{ "id": "2", "title": "another title", "views": 200 }
],
"comments": [
{ "id": "1", "text": "a comment about post 1", "postId": "1" },
{ "id": "2", "text": "another comment about post 1", "postId": "1" }
],
"profile": {
"name": "typicode"
}
}
{
posts: [
{ id: '1', title: 'a title', views: 100 },
{ id: '2', title: 'another title', views: 200 },
],
comments: [
{ id: '1', text: 'a comment about post 1', postId: '1' },
{ id: '2', text: 'another comment about post 1', postId: '1' },
],
profile: {
name: 'typicode',
},
}
You can read more about JSON5 format here.
Pass it to JSON Server CLI
$ npx json-server db.json
Get a REST API
$ curl http://localhost:3000/posts/1
{
"id": "1",
"title": "a title",
"views": 100
}
Run json-server --help
for a list of options
Sponsors |
---|
Sponsors |
---|
Sponsors | |
---|---|
Become a sponsor and have your company logo here
[!NOTE] This project uses the Fair Source License. Only organizations with 3+ users are kindly asked to contribute a small amount through sponsorship sponsor for usage. This license helps keep the project sustainable and healthy, benefiting everyone.
For more information, FAQs, and the rationale behind this, visit https://fair.io/.
Based on the example db.json
, you'll get the following routes:
GET /posts
GET /posts/:id
POST /posts
PUT /posts/:id
PATCH /posts/:id
DELETE /posts/:id
# Same for comments
GET /profile
PUT /profile
PATCH /profile
→ ==
lt
→ <
lte
→ <=
gt
→ >
gte
→ >=
ne
→ !=
GET /posts?views_gt=9000
start
end
limit
GET /posts?_start=10&_end=20
GET /posts?_start=10&_limit=10
page
per_page
(default = 10)GET /posts?_page=1&_per_page=25
_sort=f1,f2
GET /posts?_sort=id,-views
x.y.z...
x.y.z[i]...
GET /foo?a.b=bar
GET /foo?x.y_lt=100
GET /foo?arr[0]=bar
GET /posts?_embed=comments
GET /comments?_embed=post
DELETE /posts/1
DELETE /posts/1?_dependent=comments
If you create a ./public
directory, JSON Server will serve its content in addition to the REST API.
You can also add custom directories using -s/--static
option.
json-server -s ./static
json-server -s ./static -s ./node_modules
id
is always a string and will be generated for you if missing_per_page
with _page
instead of _limit
for paginationNetwork tab > throtling
to delay requests instead of --delay
CLI optionFAQs
[![Node.js CI](https://github.com/typicode/json-server/actions/workflows/node.js.yml/badge.svg)](https://github.com/typicode/json-server/actions/workflows/node.js.yml)
The npm package json-server receives a total of 213,990 weekly downloads. As such, json-server popularity was classified as popular.
We found that json-server demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.