5、faker.js数据模拟
今天发现了一个神器——json-server!在他的帮助下可以在很短的时间内搭建一个Rest API, 然后就可以让前端在不依赖后端的情况下进行开发啦!
关于什么是RESTful API:
《RESTful API 设计指南》—— 阮一峰
http://www.ruanyifeng.com/blo...
JSON-Server
简单来说,JSON-Server是一个Node模块,运行Express服务器,你可以指定一个json文件作为api的数据源。
举个例子:
我们现在想做一个app,用来管理客户信息,实现简单的CRUD功能(create/retrieve/update/delete),比如:
获取客户信息
增加一个客户
删除一个客户
更新客户信息
好啦,接下来我们就使用json-server完成这一系列动作吧!
安装JSON-Server
npm install -g json-server //osx系统加'sudo'
新建一个文件夹同时cd它:
mkdir customer-manager && cd customer-manager
新建一个json文件,然后存放一点数据进去:
touchcustomers.json
{
"customers": [
{ "id": 1, "first_name": "John", "last_name": "Smith", "phone": "219-839-2819" }
]
}
开启json-server功能
所有你要做的事情只是让json-server指向这个customers.json就ok啦!
json-server customers.js
然后出现这个提示就ok啦!
另外,JSON-Server很酷的一点就是支持各种GET/POST/PUT/DELETE的请求。
看几个例子:
//GET
fetch('http://localhost:3000/tasks/')
.then(function(response) {return response.json()
}).then(function(json) {
console.log('parsed json: ', json)
}).catch(function(ex) {
console.log('parsing failed: ', ex)
});
//POST
fetch('http://localhost:3000/tasks/', {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
"title": "Add a blogpost about Angular2",
"dueDate": "2015-05-23T18:25:43.511Z",
"done": false
})
}).then(function(response) {return response.json()
}).then(function(json) {
console.log('parsed json: ', json)
}).catch(function(ex) {
console.log('parsing failed: ', ex)
});
//PUT
fetch('http://localhost:3000/tasks/1', { //在url后面指定下id就好
method: 'put',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
"done": true
})
}).then(function(response) {return response.json()
}).then(function(json) {
console.log('parsed json: ', json)
}).catch(function(ex) {
console.log('parsing failed: ', ex)
});
//DELETE
fetch('http://localhost:3000/tasks/1', {
method: 'delete'
}).then(function(response) {return response.json()
}).then(function(json) {
console.log('parsed json: ', json)
}).catch(function(ex) {
console.log('parsing failed: ', ex)
});
JSON-Server基本就是这样啦!接下来介绍另一个神器~
Faker.js
如果要自己瞎编API数据的话也是比较烦恼,用faker.js就可以轻松解决这个问题啦!他可以帮助你自动生成大量fake的json数据,作为后端数据~
安装faker.js
还是使用npm来安装faker.js:
npm install faker
现在我们用javascript生成一个包含50个客户数据的json文件:
//customers.js
var faker = require('faker')
functiongenerateCustomers () {
var customers = []
for (var id = 0; id < 50; id++) {
var firstName = faker.name.firstName()
var lastName = faker.name.firstName()
var phoneNumber = faker.phone.phoneNumberFormat()
customers.push({
"id": id,
"first_name": firstName,
"last_name": lastName,
"phone": phoneNumber
})
}
return { "customers": customers }
}
// 如果你要用json-server的话,就需要export这个生成fake data的function
module.exports = generateCustomers
然后让json-server指向这个js文件:
json-server customers.js
这样你就可以在http://localhost:3000/customers里看到50个客户数据了。
更多faker.js属性可以查看这里:
https://github.com/marak/Fake...
好啦,基本就是这样啦,happy coding!
最新文章
- 通用PE工具箱 4.0精简优化版
- Git Sophisticated Commands
- 由于启动用户实例的进程时出错,导致无法生成 SQL Server 的用户实例。该连接将关闭。
- 文件操作 fopen() fclose()
- HTML--4格式布局
- python黑帽子源码
- Codeforces Round #327 (Div. 1) B. Chip &#39;n Dale Rescue Rangers 二分
- 1501 二叉树最大宽度和高度 (BFS+树的遍历)
- java核心技术学习笔记之一程序设计环境
- 计算机程序的思维逻辑 (82) - 理解ThreadLocal
- 【ShaderToy】画一个球体
- 使用Python画一个带坐标轴的圆
- Android开发——Android进程保活招式大全
- 在ASP.NET MVC4中实现同页面增删改查,无弹出框01,Repository的搭建
- Asp.net MVC + Redis(Linux安装Redis)
- PASSWORD MySQL 5.6.21-1ubuntu14.04_amd64
- about SpringBoot学习后记
- linux下安装php扩展amqp
- WORD中无损复制图片
- java网络编程客户端与服务端原理以及用URL解析HTTP协议
热门文章
- 第十五节:深入理解async和await的作用及各种适用场景和用法
- Geometric regularity criterion for NSE: the cross product of velocity and vorticity 1: $u\times \om$
- MySQL学习9 - 单表查询
- 第29月第2天 charles
- 410 for 循环 运算 改变循环的控制流 死循环 遍历数组 定义方法 有名函数匿名函数 定义函数的方法取值 date math 局部变量 函数 局部与全局变量 次幂/随机数/取绝对值/向上取整/平方根
- Django组件-forms
- QML ListView json
- C# 在webapi项目中配置Swagger
- echarts tree 树型图层级距离设置
- Shell的类型