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!
5、faker.js数据模拟的更多相关文章
- Mock.js数据模拟
数据来源方式: 为什么要用mockjs 实际开发中,前后端分离,前端需要后端的接口去完成页面的渲染,但是并不能等到后端成员写完接口再开始进行测试.大部分情况下,前后端需要同时进行开发.因此便需要moc ...
- ssr.js数据模拟工具
ssr相当于是搭建了一个 Mock Server ,构建假数据,然后把这些假数据存到 JSON 文件上,Mock Server 可以响应请求或者生成页面,当然也可以顺便生成 API 文档. 强制跨域访 ...
- vue前后分离---数据模拟
最近为在做CRM的前期工作,忙里偷闲写了个关于数据模拟方面的东西 主要是现在博客中满天都再说前后分离,但是还没有几个实际操作的---让许多新手{-_-} 方法一: 启动一个express静态服务器-- ...
- 你需要了解的JS框架
excanvas.js/Chart.js/cubism.js/d3.js/dc.js/dx.chartjs.js/echarts.js/flot.js 用途:构建数据统计图表,兼容多浏览器 ...
- 前端开发需要了解的JS插件
excanvas.js/Chart.js/cubism.js/d3.js/dc.js/dx.chartjs.js/echarts.js/flot.js 用途:构建数据统计图表,兼容多浏览器 jquer ...
- vue-cli 本地数据模拟
方法一: 使用express搭建静态服务 mock数据写在json文件中,proxyTable 里将接口代理到具体mock数据json文件上.具体方法: 创建 mock 文件夹 build/dev-s ...
- 使用Mock.js进行独立于后端的前端开发
Mockjs能做什么? 基于 数据模板 生成模拟数据. 基于 HTML模板 生成模拟数据. 拦截并模拟 ajax 请求. 能解决的问题 开发时,前后端进度不同步,后端还没完成数据输出,前端只好写静态模 ...
- Vue.js(15)之 json-server搭建模拟的API服务器
json-server搭建模拟的API服务器 运行命令 npm install json-server -D 全局安装 json-server 项目根目录下创建 mock 文件夹 mock 文件夹下添 ...
- js 一个或多个一维数组,算出元素之间相互组合的所有情况
// 数据源 var target = { state1: ['1', '2'], state2: ['01', '02', '03'], state3: ['001','002'] } stackS ...
随机推荐
- 第六节:深入研究Task实例方法ContinueWith的参数TaskContinuationOptions
一. 整体说明 揭秘: 该章节的性质和上一个章节类似,也是一个扩展的章节,主要来研究Task类下的实例方法ContinueWith中的参数TaskContinuationOptions. 通过F12查 ...
- 13、 使用openpyxl存储周杰伦的歌曲信息
import requests import openpyxl res = requests.get('https://c.y.qq.com/soso/fcgi-bin/client_search ...
- [物理学与PDEs]第2章第2节 粘性流体力学方程组 2.4 粘性热传导流体动力学方程组
粘性热传导流体动力学方程组: $$\beex \bea \cfrac{\p \rho}{\p t}+\Div(\rho{\bf u})&=0,\\ \rho \cfrac{\rd {\bf u ...
- HT for Web框架使用心得
一.简单介绍 在HT for Web的官网首页写着,构建先进2D和3D可视化所需要的一切. 是的,只要你看过官网,你就会知道,这是一个企业的.并非开源的且需要收费的框架. 因为公司的业务需要,且公司使 ...
- [转] Python 字符编码判断
转自:http://www.cnblogs.com/dkblog/archive/2011/03/02/1980644.html 法一: isinstance(s, str) 用来判断是否为一般字符串 ...
- 【原创】大叔经验分享(45)kibana添加index pattern卡住 返回403 Forbidden
kibana添加index pattern卡住,通过浏览器查看请求返回状态为403 Forbidden,返回消息为: {"message":"blocked by: [F ...
- php-beast 代码加密
https://github.com/liexusong/php-beast 修改php.ini配置 extension_dir = "/usr/lib/php/20151012/" ...
- 《剑指offer》丑数
本题来自<剑指offer> 反转链表 题目: 思路: C++ Code: Python Code: 总结:
- HBase2.0.5 WordCount
待计算的wordCount文件放在HDFS上. wc.txt: hive hadoop hello hello world hbase hive 目标:进行WordCount计算,把结果输出到HBas ...
- [转]一个普通IT人的十年回顾---金旭亮
金旭亮老师十年体会,很有收获.转自网络. 金旭亮于1989年与超级解霸的开发者梁肇新同时迈入广西大学的校门,却走了一条与其不同的路. 1994年起开始自学计算机专业本科课程,并开始编程,从未间断,迄今 ...