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

mock 数据( json-server )

json-server 实现数据 mock

实现步骤:

1. 在项目中安装 json-server

npm install -D json-server

2. 准备一个 json 文件

   server/data.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"}
}

3. 添加启动命令

"scripts":{"server": "json-server ./server/data.json --port 8888"
}

4. 访问接口进行测试

npm run serve
  const [commentList, setCommentList] = useState([]);useEffect(() => {async function getList() {const res = await axios.get("http://localhost:8888");setCommentList(res.data);}getList();}, []);

http://www.xdnf.cn/news/6305.html

相关文章:

  • React学习———Redux 、 React Redux和react-persist
  • <C++> MFC自动关闭对话框(MessageBoxTimeout)
  • Vue 学习随笔系列二十三 -- el-date-picker 组件
  • Python中的虚拟环境
  • U9C与钉钉审批流对接完整过程
  • lesson02-PyTorch开发环境安装
  • 光谱相机的图像预处理技术
  • 当服务器出现宕机情况该怎么办?
  • Axure中继器高保真交互原型的核心元件
  • 科技的成就(六十八)
  • 网络原理 | 网络基础概念复习
  • Automatic Recovery of the Atmospheric Light in Hazy Images论文阅读
  • 使用 SiamMask 实现单目标逐帧跟踪与掩码中心提取
  • 深入探索 OpenCV:从实时视频流到图像处理的实战指南
  • 技术文档不完善,如何促进知识传承
  • AI日报 · 2025年5月15日|GPT-4.1 登陆 ChatGPT
  • [原创](现代Delphi 12指南):[macOS 64bit App开发]: 注意“回车换行“的跨平台使用.
  • 基于YOLO算法的目标检测系统实现指南
  • 蓝桥杯11届国B 约数
  • 小程序映射逻辑处理
  • 【Python】在vscode利用pyinstaller中的.spec文件把py项目打包为.exe实现非py环境下使用的操作步骤
  • 如何通过partclone克隆Ubuntu 22系统
  • 红黑树和递归树
  • 嵌入式学习的第二十一天-数据结构-双向链表
  • 滑动窗口最大值
  • shell脚本练习(6):备份MySQL数据库表
  • 平滑过滤值策略
  • IP地址、端口、TCP介绍、socket介绍、程序中socket管理
  • 【MySQL】第四弹——表的CRUD进阶(二)数据库设计
  • 穿透工具如何保证信息安全?