1、前言

为了前端项目获取数据,需要在本地搭建json-server,这样保证可以在本地实现增删改查的操作。

2、安装

全局安装:
npm -g json-server

3、创建一个json-server所在文件夹与文件

WebstormProjects Gavin$ mkdir jsonserver
WebstormProjects Gavin$ cd jsonserver/
jsonserver Gavin$ npm init  --yes

4、安装json-server模块

jsonserver Gavin$ cnpm install --save json-server

5、调整json-server启动方式,修改package.json文件

"scripts": {
    "json:server": "json-server --watch db.json"
  },

6、创建对应的db.json文件

{
  "users":[
    {
      "name": "Gavin",
      "phone": "333-444-555",
      "email": "gavin@gmail.com",
      "id":1,
      "age":15,
      "companyId":1
    },
    {
      "name": "Henry",
      "phone": "222-444-555",
      "email": "henry@gmail.com",
      "id":2,
      "age":20,
      "companyId":1
    },
    {
      "name": "Tom",
      "phone": "444-33-555",
      "email": "tom@gmail.com",
      "id":2,
      "age":30,
      "companyId":2
    },
    {
      "name": "Jhon",
      "phone": "333-444-333",
      "email": "jhon@gmail.com",
      "id":2,
      "age":15,
      "companyId":3
    }
  ],
  "companies":[
    {
      "id":1,
      "name": "Apple",
      "description": "Apple is good"
    },
    {
      "id":2,
      "name": "Google",
      "description": "Google is good"
    },
    {
      "id":3,
      "name": "IBM",
      "description": "IBM is good"
    }
    
  ]
}
View Code

相关文章: