React生产环境下使用mock.js

server/2025/2/21 22:28:25/

最近项目中有个需求,甲方要求在生产环境中使用mock.js数据展示前端项目,因为后端接口暂时没有。我的项目是通过vite构建的,前端项目在打包后一般不会将mock代码数据打包到dist文件夹中进而也不会调用mock数据,所以导致前端项目部署到nginx上后接口全部都报错。

1.项目下载依赖

npm install mockjs

npm install vite-plugin-mock

2.修改vite.config.js文件

javascript">      viteMockServe({mockPath: './mock', // 使用相对路径localEnabled: command === 'serve', // 保证开发阶段可以使用mock接口prodEnabled: true, // 保证生产阶段也可以使用mock接口watchFiles: true, // 监听 mock 文件变化logger: true, // 启用日志输出injectCode: `import { setupProdMockServer } from './mockProdServer';setupProdMockServer();`,injectFile:path.resolve('./src/main.jsx')}),],

3.项目根目录创建mockProdServer.js

javascript">// mockProdServer.js
import { createProdMockServer } from 'vite-plugin-mock/es/createProdMockServer';
import homeModule from './mock/home';export function setupProdMockServer() {createProdMockServer([...homeModule]);
}

4.项目根目录mock/home.js文件代码

javascript">//用户数据
const userList = [{userId: 1,avatar:'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',username: 'admin',password: '111111',token: 'Admin Token',examLink:'https://www.baidu.com/',},{userId: 2,avatar:'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',username: 'system',password: '111111',token: 'System Token',examLink:'https://www.baidu.com/',},
]
// 课程章节数据
// 菜单项务必填写 key label 是必须的 key 不能重复
const chapterList = [{label: '第一节', key: '1', children: [{key: '1-1',label: '第一节第一课时',},{key: '1-2',label: '第一节第二课时',},]}, {label: '第二节', key: '2', children: [{key: '2-1',label: '第二节第一课时',},{key: '2-2',label: '第二节第二课时',},]},{label: '第三节',key: '3',children: [{key: '3-1',label: '第三节第一课时',},{key: '3-2',label: '第三节第二课时',},],},{label: '第四节',key: '4',children: [{key: '4-1',label: '第四节第一课时',},{key: '4-2',label: '第四节第二课时',},],},{label: '第五节',key: '5',children: [{key: '5-1',label: '第五节第一课时',},{key: '5-2',label: '第五节第二课时',},],},
]
//推荐内容
const recommendList = [{avatar: 'https://joeschmoe.io/api/v1/random',title: 'Ant Design Title 1',description: 'Ant Design, a design...'},{avatar: 'https://joeschmoe.io/api/v1/random',title: 'Ant Design Title 2',description: 'Ant Design, a design...'},{avatar: 'https://joeschmoe.io/api/v1/random',title: 'Ant Design Title 3',description: 'Ant Design, a design...'},{avatar: 'https://joeschmoe.io/api/v1/random',title: 'Ant Design Title 4',description: 'Ant Design, a design...'},{avatar: 'https://joeschmoe.io/api/v1/random',title: 'Ant Design Title 4',description: 'Ant Design, a design...'},{avatar: 'https://joeschmoe.io/api/v1/random',title: 'Ant Design Title 4',description: 'Ant Design, a design...'},{avatar: 'https://joeschmoe.io/api/v1/random',title: 'Ant Design Title 4',description: 'Ant Design, a design...'},{avatar: 'https://joeschmoe.io/api/v1/random',title: 'Ant Design Title 4',description: 'Ant Design, a design...'},{avatar: 'https://joeschmoe.io/api/v1/random',title: 'Ant Design Title 4',description: 'Ant Design, a design...'},{avatar: 'https://joeschmoe.io/api/v1/random',title: 'Ant Design Title 4',description: 'Ant Design, a design...'}
]//课程内容 key对应章节key
const courseContent = [{key:'1-1',pdfUrl:'/test.pdf',videoUrl:'https://media.w3.org/2010/05/sintel/trailer_hd.mp4',},{key:'1-2',pdfUrl:'/test1.pdf',videoUrl:'https://media.w3.org/2010/05/sintel/trailer_hd.mp4',},{key:'2-1',pdfUrl:'/test.pdf',videoUrl:'https://media.w3.org/2010/05/sintel/trailer_hd.mp4',},
]export default [// 获取用户信息{url: '/api/user/info',method: 'get',response: (request) => {//获取请求头携带tokenconst token = request.headers.token;//查看用户信息是否包含有次token用户const checkUser = userList.find((item) => item.token === token)//没有返回失败的信息if (!checkUser) {return { code: 201, data: { message: '获取用户信息失败' } }}//如果有返回成功信息return { code: 200, data: { checkUser } }},},// 获取课程章节{url: '/api/chapter/list',method: 'get',response: (request) => {//获取请求头携带tokenconst token = request.headers.token;if (!token) {return { code: 201, data: { message: '获取用户信息失败' } }}return { code: 200, data: { chapterList } }},},// 获取推荐内容{url: '/api/recommend/list',method: 'get',response: (request) => {//获取请求头携带tokenconst token = request.headers.token;if (!token) {return { code: 201, data: { message: '获取用户信息失败' } }}return { code: 200, data: { recommendList } }},},// 获取课程内容{url: '/api/course/content',method: 'post',response: (request) => {//获取请求头携带tokenconst token = request.headers.token;const { key } = request.body;if (!token) {return { code: 201, data: { message: '获取用户信息失败' } }}const contentObj = courseContent.find((item) => item.key === key,)return { code: 200, data: { contentObj } }},}
]

5.项目main.jsx

javascript">import {setupProdMockServer} from '../mockProdServer'
setupProdMockServer()

这样在运行npm run build后项目部署到nginx上后接口在请求mock数据后接口依然可以获取到数据


http://www.ppmy.cn/server/169176.html

相关文章

Rook-ceph(1.92最新版)

安装前准备 #确认安装lvm2 yum install lvm2 -y #启用rbd模块 modprobe rbd cat > /etc/rc.sysinit << EOF #!/bin/bash for file in /etc/sysconfig/modules/*.modules do[ -x \$file ] && \$file done EOF cat > /etc/sysconfig/modules/rbd.modules &l…

【Spring详解一】Spring整体架构和环境搭建

一、Spring整体架构和环境搭建 1.1 Spring的整体架构 Spring框架是一个分层架构&#xff0c;包含一系列功能要素&#xff0c;被分为大约20个模块 Spring核心容器&#xff1a;包含Core、Bean、Context、Expression Language模块 Core &#xff1a;其他组件的基本核心&#xff…

【设计模式】 代理模式(静态代理、动态代理{JDK动态代理、JDK动态代理与CGLIB动态代理的区别})

代理模式 代理模式是一种结构型设计模式&#xff0c;它提供了一种替代访问的方法&#xff0c;即通过代理对象来间接访问目标对象。代理模式可以在不改变原始类代码的情况下&#xff0c;增加额外的功能&#xff0c;如权限控制、日志记录等。 静态代理 静态代理是指创建的或特…

Office hour 2-自然语言处理

主要涉及**自然语言处理&#xff08;NLP&#xff09;**的多个方面&#xff0c;包括发展历程、神经网络模型、大语言模型、以及实际应用。 01 - NLP的发展历程 1. 1950 - 1969&#xff1a; • 机器翻译研究&#xff1a;NLP的研究始于机器翻译&#xff0c;探索计算机如何处理和…

以用户为中心,汽车 HMI 界面设计的创新之道

在汽车智能化飞速发展的当下&#xff0c;汽车 HMI&#xff08;人机交互界面&#xff09;成为连接人与车的关键桥梁。如何打造出优秀的 HMI 界面&#xff1f;答案是以用户为中心&#xff0c;探索创新之道。 用户需求是汽车 HMI 界面设计的指南针。在设计前期&#xff0c;深入调…

Spring-GPT智谱清言AI项目(附源码)

一、项目介绍 本项目是Spring AI第三方调用整合智谱请言&#xff08;官网是&#xff1a;https://open.bigmodel.cn&#xff09;的案例&#xff0c;回答响应流式输出显示&#xff0c;这里使用的是免费模型&#xff0c;需要其他模型可以去 https://www.bigmodel.cn/pricing 切换…

深入理解Redis

一、引言 在当今高并发、大数据量的互联网应用场景中&#xff0c;提升系统性能和响应速度是后端开发的关键挑战之一。Redis作为一款高性能的内存数据库&#xff0c;以其出色的读写速度、丰富的数据结构和强大的功能特性&#xff0c;成为Java后端开发中不可或缺的缓存和数据存储…

Spring Boot 实战:轻松实现文件上传与下载功能

目录 一、引言 二、Spring Boot 文件上传基础 &#xff08;一&#xff09;依赖引入 &#xff08;二&#xff09;配置文件设置 &#xff08;三&#xff09;文件上传接口编写 &#xff08;一&#xff09;文件类型限制 &#xff08;二&#xff09;文件大小验证 &#xff0…