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 ...
随机推荐
- ArcGis Python脚本——根据接图表批量裁切分幅影像
年前写了一个用渔网工具制作图幅接图表的文章,链接在这里: 使用ArcMap做一个1:5000标准分幅图并编号 本文提供一个使用ArcMap利用接图表图斑裁切一幅影像为多幅的方法. 第一步,将接图表拆分 ...
- Lookup dict 并将属性更新于lookupdict object中
# encoding:utf-8class LookupDict(dict): """Dictionary lookup object.""" ...
- 代码,java_web
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- day 18 - 1 正则与 re 模块
正则表达式 官方定义:正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个 “规则字符串”,这个 “规则字符串” 用来表达对字符串的一种过滤逻辑. 我 ...
- CapsNet胶囊网络(理解)
0 - 背景 Geoffrey Hinton是深度学习的开创者之一,反向传播等神经网络经典算法发明人,他在去年年底和他的团队发表了两篇论文,介绍了一种全新的神经网络,这种网络基于一种称为胶囊(caps ...
- 基于深度学习的目标检测技术演进:R-CNN、Fast R-CNN、Faster R-CNN
object detection我的理解,就是在给定的图片中精确找到物体所在位置,并标注出物体的类别.object detection要解决的问题就是物体在哪里,是什么这整个流程的问题.然而,这个问题 ...
- installshield中杀死某一个进程
///////////////////////////////////////////////// // Function prototypes. ////////////////////////// ...
- 在python中使用print()时,raw write()返回无效的长度:OSError: raw write() returned invalid length 254 (should have been between 0 and 127)
写出一个不是code的bug,很烦恼,解决了挺长时间,都翻到外文来看,不过还是解决了,只尝试了一种简单可观的方法,希望对大家有用 我正在使用Django与Keras(tensorflow)来训练一个模 ...
- 连接慢的主要原因是DNS解析导致
连接慢的主要原因是DNS解析导致解决方法: 1.在ssh服务端上更改/etc/ssh/sshd_config文件中的配置为如下内容:UseDNS no# GSSAPI optionsGSSAPIAut ...
- Xshell 连接 vmware中的CentOS 7
参考内容: Xshell 连接 CentOS 7 与 Ubuntu Server,http://www.linuxidc.com/Linux/2017-03/141333.ht ...