• Stars
    star
    72,441
  • Rank 103 (Top 0.01 %)
  • Language
    JavaScript
  • License
    Other
  • Created almost 11 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Get a full fake REST API with zero coding in less than 30 seconds (seriously)

json-server

Node.js CI

Important

Viewing alpha v1 documentation – usable but expect breaking changes. For stable version, see here

Install

npm install json-server

Usage

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"
  }
}
View db.json5 example
{
  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"
}

Run json-server --help for a list of options

Sponsors ✨

Sponsors

Become a sponsor and have your company logo here

Sponsorware

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/.

Routes

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

Params

Conditions

  • ==
  • lt<
  • lte<=
  • gt>
  • gte>=
  • ne!=
GET /posts?views_gt=9000

Range

  • start
  • end
  • limit
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

  • _sort=f1,f2
GET /posts?_sort=id,-views

Nested and array fields

  • x.y.z...
  • x.y.z[i]...
GET /foo?a.b=bar
GET /foo?x.y_lt=100
GET /foo?arr[0]=bar

Embed

GET /posts?_embed=comments
GET /comments?_embed=post

Delete

DELETE /posts/1
DELETE /posts/1?_dependent=comments

Serving static files

If you create a ./public directory, JSON Serve 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

Notable differences with v0.17

  • id is always a string and will be generated for you if missing
  • use _per_page with _page instead of _limitfor pagination
  • use Chrome's Network tab > throtling to delay requests instead of --delay CLI option

More Repositories

1

husky

Git hooks made easy 🐶 woof!
JavaScript
32,250
star
2

lowdb

Simple and fast JSON database
JavaScript
21,308
star
3

hotel

🏩 A simple process manager for developers. Start apps from your browser and access them using local domains
JavaScript
9,975
star
4

jsonplaceholder

A simple online fake REST API server
HTML
5,017
star
5

tlapse

📷 Create a timelapse of your web development... or just automatically take screenshots of your hard work ;)
JavaScript
2,024
star
6

xv

🙅‍♀️ ✌️ fastest test runner
JavaScript
832
star
7

mistcss

💧 Write atomic components using only CSS! (JS-from-CSS™)
JavaScript
788
star
8

pegasus

Load JSON while still loading other scripts
JavaScript
703
star
9

katon

(use hotel instead)
JavaScript
684
star
10

steno

Super fast async file writer with atomic write ⚡
JavaScript
678
star
11

react-fake-props

🔮 Magically generate fake props for your React tests
JavaScript
627
star
12

fetchival

Easy window.fetch requests
JavaScript
520
star
13

lodash-id

Makes it easy to manipulate id-based resources with lodash or lowdb
JavaScript
472
star
14

react-lodash

⚛️ 🔧 Lodash as React components
JavaScript
356
star
15

stop-server

📱 Shut down your computer with your phone
JavaScript
349
star
16

demo

A demo repository for My JSON Server (Alpha)
334
star
17

pinst

🍺 dev only postinstall hooks (package.json)
JavaScript
258
star
18

please-upgrade-node

💁 Show a message to your users to upgrade Node instead of a stacktrace
JavaScript
239
star
19

jsop

JSON file reader/writer (powered by Object.observe)
JavaScript
207
star
20

husky-4-to-8

Quickly migrate your hooks from husky v4 to husky@latest
JavaScript
131
star
21

user-startup

Auto start commands when you log in (cross-platform)
JavaScript
125
star
22

cult

cult monitors gulpfile changes and reloads gulp
JavaScript
119
star
23

bg.nvim

Automatically sync your terminal background with your colorscheme 🎆
Lua
97
star
24

logan

Mini template system for the console and colors
JavaScript
81
star
25

minihost

Easily start and access servers
JavaScript
46
star
26

ghwn

Get desktop notifications for new issues, comments, stars... (no installation required)
HTML
42
star
27

shoutjs

Make your ShellJS commands explicit and get a beautiful output
JavaScript
30
star
28

homerun

Turn npm package scripts into CLI commands
JavaScript
23
star
29

server-ready

Know when a server is ready to receive requests
JavaScript
20
star
30

husky-init

JavaScript
20
star
31

backbone-pegasus

Load models and collections data while loading Backbone
JavaScript
20
star
32

server-ready-cli

Run commands only when a server is available
JavaScript
19
star
33

eslint-config

JavaScript
8
star
34

typicode.github.io

HTML
8
star