当前位置: 首页 > news >正文

2025第3周 | json-server的基本使用

目录

  • 1. json-server是什么?
  • 2. json-server怎么用?
    • 2.1 安装
    • 2.2 创建db.json
    • 2.3 启动服务
    • 2.4 查看效果
  • 3. 前端进行模拟交互
    • 3.1 创建demo.html
    • 3.2 创建demo.js

2025,做想做的事,读想读的书,持续学习,自律生活,修行人生。

2025一个转身就已到来,对于时间太快,总会有种惶恐感。每每回首过去,总会发现想要做的事情没有完成,学习的内容少之又少,读的有用的书籍更是可以忽略不计。对于内在的超越,内在的修行也是没有始终践行,知行合一,实践、认识、再实践、再认识…

2025年第2周:2025.01.13 ~ 2025.01.19 ,16号星期四

目标:了解 json-server基本使用

  • json-server是什么?
  • json-server怎么用?
  • 前端进行模拟交互

1. json-server是什么?

Json-server 是一个无需编写任何代码,便可快速搭建本地 RESTful API 的工具。专为需要快速后端进行原型设计和模拟的前端开发人员而创建。这大大方便了前端开发人员,需要做一些与后台的交互。

  • github地址:http://github.com/typicode/json-server

2. json-server怎么用?

2.1 安装

执行如下命令安装 json-server

npm install json-server -g

查看是否安装成功:

json-server --version

2.2 创建db.json

在你项目根目录下创建文件db.json ,内容如下:

{"posts": [{ "id": "1", "title": "a title", "views": 100 },{ "id": "2", "title": "another title", "views": 200 }],"comments": [{ "id": "1", "text": "a comment about post 1", "postId": "1" },{ "id": "2", "text": "another comment about post 1", "postId": "1" }],"profile": {"name": "typicode"}
}

2.3 启动服务

执行如下命令,启动服务

# 本地
npx json-server db.json
# 全局
json-server db.json

启动成功,会显示如下信息:

PS D:\MyWork\vscode\axios> npx json-server db.json
JSON Server started on PORT :3000
Press CTRL-C to stop
Watching db.json...(˶ᵔ ᵕ ᵔ˶)Index:
http://localhost:3000/Static files:
Serving ./public directory if it existsEndpoints:
http://localhost:3000/posts
http://localhost:3000/comments
http://localhost:3000/profile

2.4 查看效果

打开浏览器输入链接查看效果,比如查看id为1的文章

http://localhost:3000/posts/1

一些简单API

GET    /posts
GET    /posts/:id
POST   /posts			(新增)
PUT    /posts/:id	    (更新)
PATCH  /posts/:id
DELETE /posts/:id

3. 前端进行模拟交互

使用axios模拟get请求,比如我们需要查看id1的内容,代码如下所示:

3.1 创建demo.html


<div class="container"><button class="btn btn-primary">使用axios模拟get请求</button>
</div>
<script src="./js/demo.js"></script>

3.2 创建demo.js

const btns = document.querySelectorAll('button');btns[0].onclick = function() {// 发送ajax请求axios({method: 'get',url: 'http://localhost:3000/posts/1',}).then(res => {// {id: '1', title: 'a title', views: 100}console.log(res.data);  })
}
http://www.lryc.cn/news/522276.html

相关文章:

  • Autodl转发端口,在本地机器上运行Autodl服务器中的ipynb文件
  • flutter Get GetMiddleware 中间件不起作用问题
  • RabbitMQ(三)
  • 【Python】Python之locust压测教程+从0到1demo:基础轻量级压测实战(1)
  • 【JavaScript】基础内容,HTML如何引用JavaScript, JS 常用的数据类型
  • vue使用自动化导入api插件unplugin-auto-import,避免频繁手动导入
  • 在 C# 中的Lambda 表达式
  • 奉加微PHY6230兼容性:部分手机不兼容
  • 32单片机综合应用案例——基于GPS的车辆追踪器(三)(内附详细代码讲解!!!)
  • 45_Lua模块与包
  • 深度学习电影推荐-CNN算法
  • 【Git 】探索 Git 的魔法——git am 与补丁文件的故事
  • G1原理—5.G1垃圾回收过程之Mixed GC
  • 机器人传动力系统介绍
  • 1161 Merging Linked Lists (25)
  • 内联变量(inline variables):在多个文件中共享全局常量
  • Jmeter进行http接口并发测试
  • 力扣解题汇总_JAVA
  • ubuntu下安装编译cmake,grpc与protobuf
  • SQL Prompt 插件
  • 知识图谱抽取分析中,如何做好实体对齐?
  • 【Python通过UDP协议传输视频数据】(界面识别)
  • 【伪随机数】关于排序算法自测如何生成随机数而引发的……
  • 核密度估计(Kernel Density Estimation, KDE)是一种非参数统计方法
  • 【k8s面试题2025】2、练气初期
  • 栈溢出原理
  • Jmeter如何进行多服务器远程测试
  • 2.slf4j入口
  • 初学stm32 --- CAN
  • 软件测试—接口测试面试题及jmeter面试题