Skip to the content.

Create Demo API with JSON Server

Installation

npm install -g json-server

Usage

First Way

{
  "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"
  }
}
json-server db.json
curl http://localhost:3000/profile/
#{
#  "name": "typicode"
#}
curl http://localhost:3000/posts/
#[
#  {
#    "id": "1",
#    "title": "a title",
#    "views": 100
#  },
#  {
#    "id": "2",
#    "title": "another title",
#    "views": 200
#  }
#]
curl http://localhost:3000/comments/
#[
#  {
#    "id": "1",
#    "text": "a comment about post 1",
#    "postId": "1"
#  },
#  {
#    "id": "2",
#    "text": "another comment about post 1",
#    "postId": "1"
#  }
#]

Second Way

module.exports = function (){
  var faker = require('faker');
  var _ = require('lodash');
  return {
    people: _.times(100, function(n){
      return {
        id: n,
        name: faker.name.findName(),
        email: faker.internet.email(),
        avatar: faker.image.avatar()
      }
    })
  }
}
json-server generate.js

Ref: