JSON Server: 无需任何代码,30秒搭建全功能的REST API。
Faker: 快速生成大量的模拟数据
JSON Server 项目地址
Faker 项目地址
什么东东?
JSON Server 是一个后端的原型工具,很多时候我们在开发SPA(Single Page Application)项目的时候,都需要后端提供REST API供前端访问,在开发过程中,JSON Server可以作为Mock Server存在,非常快速非常简单。
有了Mock Server还需要大量的数据,我们可以使用Faker来快速生成模拟数据。
两个神器结合,就可以快速完成有大量数据的REST API服务了。
JSON Server 怎么安装
1
2
npm install -g json-server //or
yarn global add json-server
怎么使用
新建一个 db.json 文件
在上面的json文件中我们有三个顶级段,分别是posts、comments、profile,运行下面的命令
1
json-server --watch db.json
大功告成,控制台输出:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
\{^_^}/ hi!
Loading db.json
Done
Resources
http://localhost:3000/posts
http://localhost:3000/comments
http://localhost:3000/profile
Home
http://localhost:3000
Type s + enter at any time to create a snapshot of the database
Watching...
哈哈,自动生成了三个顶级段的API端点。
尝试访问以下:
1
2
3
4
5
6
http://localhost:3000/posts //返回全部的posts数据
http://localhost:3000/posts/1 //返回第一个post
http://localhost:3000/comments //返回全部comments数据
http://localhost:3000/posts/1/comments //返回第一个post的全部comment,有没有大惊失色??
http://localhost:3000/posts/1/commnets/1 //返回第一个post的第一个comment,请注意comment中的关联关系
http://localhost:3000 //json-server的运行状态
JSON Server支持GET、POST、PUT、DELETE常见动词,满足RESTful的需求。
结合Faker快速生成模拟数据
我们不可能手工生成大量数据,结合Faker就可以快速的完成。
添加faker依赖:
1
2
3
4
5
6
yarn add faker //or
npm install faker --save
//需要lodash帮助我们少些代码
yarn add lodash //or
npm install loadsh --save
创建 database.js文件,返回faker的数据供json server使用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
module . exports = () => {
const faker = require ( 'faker/locale/zh_CN' )
const _ = require ( 'lodash' )
return {
people : _ . times ( 100 , ( n ) => {
return {
id : n ,
name : faker . name . findName (),
email : faker . internet . email (),
phone : faker . phone . phoneNumber (),
avatar : faker . internet . avatar ()
}
})
}
}
通过下面的方式运行:
1
json-server database.js
查看服务器,反回了100条数据。It’s so easy.