本地搭建接口 jsonserver

1、终端跟路径安装json-sever      命令:npm install -g json-server  (windows)

         命令:sudo npm install -g json-server  (mac)

2、创建 jsonserver  文件夹 

  2.1 初始化文件  命令:npm  init   

  2.2   初始化后  在 jsonserver  文件夹里多了个package.json  文件

3、当前文件(jsonserver文件夹)加下安装  json-server   命令:sudo npm install json-server --save    安装完后package.json里多了   

"dependencies": {
"json-server": "^0.14.0"
}

4.   修改  package.json  文件的script   部分   :

"scripts": {
"json:server": "json-server --watch db.json",
"json:server:remote":"json-server http://jsonplaceholder.typicode.com/db"
},

5、在jsonser文件夹下创建db.json 文件:

  在里面创建数据例如:

{
"users": [
{
"name": "Henry",
"phone": "333-444-555",
"email": "henry@gmail.com",
"education": "adsfasfd",
"graduationschool": "asdfasfd",
"profession": "asdfasdf",
"profile": "asdfasfdasdf",
"id": 1
},
{
"name": "adfasdf",
"phone": "fasdfasfd",
"email": "asdfasfd",
"education": "fasdfasfd",
"graduationschool": "fasfasdf",
"profession": "sdfasdfafd",
"profile": "asdfasdf",
"id": 3
},
{
"name": "米斯特吴",
"phone": "13651322451",
"email": "27732357@qq.com",
"education": "本科",
"graduationschool": "北京语言大学",
"profession": "前端开发工程师",
"profile": "大家好!",
"id": 4
},
{
"name": "asdfasfd",
"phone": "asfasf",
"email": "asdfasfd",
"id": 5
}
],
"companies": [
{
"id": 1,
"name": "Apple",
"description": "Apple is good!"
},
{
"id": 2,
"name": "Microsoft",
"description": "Microsoft is good!"
},
{
"id": 3,
"name": "Google",
"description": "Google is good!"
}
]
}

6、  运行 命令:npm run json:server  

npm run json:server:remota

结果 :运行成功

 {^_^}/ hi!

 

  Loading db.json

  Done

 

  Resources

  http://localhost:3000/users

  http://localhost:3000/companies

 

  Home

  http://localhost:3000

 

7、在浏览器中运行查看把

原文地址:https://www.cnblogs.com/jinsuo/p/9244966.html