网站首页 > 数据编程 正文
json-server是一款json数据服务器,可以对json文件、js脚本生成的json数据、远程json数据进行RESTFUL风格的增删改查操作,可以通过参数、分页、排序、全文搜索、关联查询、范围查询等进行复杂查询,对开发者特别是前端开发者是一款非常好用的开发工具。
官网:https://www.npmjs.com/package/json-server
下面我们来介绍具体的用法,
首先确保你本地安装了node.js。
安装json-server
npm install -g json-server
创建一个json文件 db.json
{
"posts": [
{ "id": 1, "title": "测试标题1", "author": "张三" },
{ "id": 2, "title": "测试标题2", "author": "李四" }
],
"comments": [
{ "id": 1, "body": "这里是内容体1", "postId": 1 },
{ "id": 2, "body": "这里是内容体2", "postId": 2 }
],
"profile": { "name": "测试json" }
}
指定json文件运行服务,命令行运行以下命令
json-server --watch db.json
服务器就这么搭建好了,就是这么的快,可能还不要3分钟。
增删改查-请求示例
请求方式遵循restful风格,
- GET-查询、
- POST-新增、
- PUT-修改、
- PATCH-修改属性、
- DELETE-删除
get请求-查询
可以看到资源路径都列在控制台里了,浏览网址http://localhost:3000/posts ,出现以下结果,posts路径就是json文件里的key
[
{
"id": 1,
"title": "测试标题1",
"author": "张三"
},
{
"id": 2,
"title": "测试标题2",
"author": "李四"
}
]
POST请求-新增
使用POST请求会添加一条新记录,如POST请求:http://localhost:3000/posts,返回新记录的id。
{
"id": 3
}
json文件也被更改
{
"posts": [
{
"id": 1,
"title": "测试标题1",
"author": "张三"
},
{
"id": 2,
"title": "测试标题2",
"author": "李四"
},
{
"id": 3
}
],
"comments": [
{
"id": 1,
"body": "这里是内容体1",
"postId": 1
},
{
"id": 2,
"body": "这里是内容体2",
"postId": 2
}
],
"profile": {
"name": "测试json"
}
}
PUT请求-修改
PUT请求会修改一条记录,如请求:http://localhost:3000/posts/3,我们在请求body里输入以下修改内容:
{
"title":"这是修改的内容",
"author":"王五"
}
json文件变成了
{
"posts": [
{
"id": 1,
"title": "测试标题1",
"author": "张三"
},
{
"id": 2,
"title": "测试标题2",
"author": "李四"
},
{
"title": "这是修改的内容",
"author": "王五",
"id": 3
}
],
"comments": [
{
"id": 1,
"body": "这里是内容体1",
"postId": 1
},
{
"id": 2,
"body": "这里是内容体2",
"postId": 2
}
],
"profile": {
"name": "测试json"
}
}
PATCH请求-修改单个属性
PATCH与PUT的区别在于,PUT的请求内容需要包含整个对象除id外的所有属性,PATCH的请求内容可以是单个属性。
比如我们PATCH请求:http://localhost:3000/posts/3 ,body为
{
"title":"这是修改的内容xxxx"
}
json文件里id为3的记录仅title属性发生改变,其他字段不变
{
"posts": [
{
"id": 1,
"title": "测试标题1",
"author": "张三"
},
{
"id": 2,
"title": "测试标题2",
"author": "李四"
},
{
"title": "这是修改的内容xxxx",
"author": "王五",
"id": 3
}
],
"comments": [
{
"id": 1,
"body": "这里是内容体1",
"postId": 1
},
{
"id": 2,
"body": "这里是内容体2",
"postId": 2
}
],
"profile": {
"name": "测试json"
}
}
DELETE请求-删除
DELETE请求会删除一条数据,如请求: http://localhost:3000/posts/3 。
删除后的json文件内容发生改变,可以看到id为3的记录没了:
{
"posts": [
{
"id": 1,
"title": "测试标题1",
"author": "张三"
},
{
"id": 2,
"title": "测试标题2",
"author": "李四"
}
],
"comments": [
{
"id": 1,
"body": "这里是内容体1",
"postId": 1
},
{
"id": 2,
"body": "这里是内容体2",
"postId": 2
}
],
"profile": {
"name": "测试json"
}
}
过滤id
浏览 http://localhost:3000/posts/1 ,代表读取post里id为1的那条数据,注意不是代表第1条,而是id为1的那条。
{
"id": 1,
"title": "测试标题1",
"author": "张三"
}
过滤参数
浏览 http://localhost:3000/posts?author=张三
[
{
"id": 1,
"title": "测试标题1",
"author": "张三"
}
]
如果有多级也可以用“.”+属性名查询如 http://localhost:3000/posts?author.name=张三。
排序
通过“_sort”指定排序属性和“_order”指定asc顺序还是desc倒序查询,如:http://localhost:3000/posts?_sort=id&_order=desc 。
分页
通过“_page”和“_limit”进行分页查询,如查询第一页,每页20条查询写法:http://localhost:3000/posts?_page=1&_limit=20
截取部分
通过“_start”、“_end”和“_limit”参数查询,如从20条开始查询10条数据:http://localhost:3000/posts/1/comments?_start=20&_limit=10
不等于查询
通过“属性名_ne”写法如:http://localhost:3000/posts?id_ne=1
[
{
"id": 2,
"title": "测试标题2",
"author": "李四"
}
]
大于等于/小于等于查询
通过“属性名_gte”查询大于等于,通过“属性名_lte”查询小于等于,写法如:http://localhost:3000/posts?views_gte=10&views_lte=20
[]
like查询
通过“属性名_like”的写法查询如http://localhost:3000/posts?title_like=标题1
[
{
"id": 1,
"title": "测试标题1",
"author": "张三"
}
]
全文搜索
通过q查询参数如: http://localhost:3000/posts?q=标题
[
{
"id": 1,
"title": "测试标题1",
"author": "张三"
},
{
"id": 2,
"title": "测试标题2",
"author": "李四"
}
]
关联查询
通过_embed参数请求如:http://localhost:3000/posts?_embed=comments ,代表posts关联comments。
[
{
"id": 1,
"title": "测试标题1",
"author": "张三",
"comments": [
{
"id": 1,
"body": "这里是内容体1",
"postId": 1
}
]
},
{
"id": 2,
"title": "测试标题2",
"author": "李四",
"comments": [
{
"id": 2,
"body": "这里是内容体2",
"postId": 2
}
]
}
]
指定端口
命令增加 --port参数
json-server --watch db.json --port 3004
使用js脚本生成数据代替json
创建一个index.js文件用于生成数据
module.exports = () => {
const data = { users: [] }
// Create 1000 users
for (let i = 0; i < 1000; i++) {
data.users.push({ id: i, name: `user${i}` })
}
return data
}
运行服务指定js文件
json-server index.js
使用远程地址加载数据
json-server http://example.com/file.json
json-server http://jsonplaceholder.typicode.com/db
作为静态文件服务器
创建一个public目录,创建一个内容为hello world的index.html文件。
mkdir public
echo 'hello world' > public/index.html
json-server db.json
访问 http://localhost:3000/ ,根路径会默认显示public目录下的内容,会显示index.html的内容。
'hello world'
也可以通过--static参数指定其他路径的目录
json-server db.json --static ./some-other-dir
自定义请求路由
创建一个 routes.json文件用于定义请求路由,每个路径都由 / 开头。
{
"/api/*": "/$1",
"/:resource/:id/show": "/:resource/:id",
"/posts/:category": "/posts?category=:category",
"/articles\\?id=:id": "/posts/:id"
}
启动服务时指定请求路由定义文件
json-server db.json --routes routes.json
以下访问左边的路径分别对应右边的原始请求效果
- /api/posts ==》 /posts
- /api/posts/1 ==》 /posts/1
- /posts/1/show ==》 /posts/1
- /posts/javascript ==》 /posts?category=javascript
- /articles?id=1 ==》 /posts/1
更多功能可以通过json-server的官网查阅,希望本文对你有所帮助。
- 上一篇: 如何高效实现汤臣倍健营销云数据集成到SQLServer
- 下一篇: 用C#开发的配方管理系统
猜你喜欢
- 2024-12-05 利用json-server搭建本地数据接口
- 2024-12-05 等保2.0数据库测评 - SQL server数据库
- 2024-12-05 用C#开发的配方管理系统
- 2024-12-05 如何高效实现汤臣倍健营销云数据集成到SQLServer
- 2024-12-05 探索SQL 与 NO-SQL 数据库-到底有啥不一样呢?
- 2024-12-05 json-server使用
- 2024-12-05 SQL Backup Master 7.2.825 SQL Server 数据库备份
- 2024-12-05 快速将数据库SQL转换为RESTful API
- 2024-12-05 SQL Server 简介
- 2024-12-05 MySQL Json有哪些缺点
- 01-15MySQL数据库语句
- 01-15如何让MySQL查询速度提升10倍以上-爱可生
- 01-15Python+MySQL数据库操作(PyMySQL)
- 01-15【数据管理】数据库通用概念和常用SQL讲解
- 01-15MySQL数据库性能优化
- 01-15怎样写出可以在各个数据库中都能执行的SQL?
- 01-15Excel常用函数1:统计类函数
- 01-15从数据库、代码层、缓存使用3个方向,聊聊如何减少bug?
- 最近发表
- 标签列表
-
- oraclepdb (60)
- vncviewermac (62)
- sqlservermax (58)
- mysqlcanal (61)
- mysql:commandnotfound (56)
- mysqlexplainfiltered (56)
- python位运算符 (59)
- linuxfind-name模糊查询文件 (60)
- centos7systemctl (76)
- mysqlgt (55)
- nc命令 (66)
- dockerfilecp (55)
- 单行子查询返回多个行解决办法 (58)
- ssh-2.0-openssh_7.4 (56)
- vue图片裁剪 (59)
- anyvideoconverterpro (62)
- pscache (58)
- hdfsfsck (63)
- nacos源码 (69)
- lambdawrapper (60)
- 安装jdk11 (60)
- 什么是聚簇索引 (62)
- 锁升级过程 (58)
- bootcdn (64)
- axurerp9mac破解版 (58)