前端项目部署

embedded/2024/10/16 2:27:10/

视频教程:前端项目部署

1、本地的服务器部署(node环境)

service.js

const express = require('express')
const app = express()
const history = require('connect-history-api-fallback')
const { createProxyMiddleware } = require('http-proxy-middleware')// 中间件设置静态资源目录
app.use(history())// 解决刷新404的问题:方法二 - 设置中间件
app.use(express.static(__dirname + '/public'))// 解决刷新404的问题:方法一 - 使用统配
// app.get('*', (req, res) => {
//   res.sendFile(__dirname + '/public/index.html')
// })// 解决前端部署以后,ajax 发送不成功的问题
/*** 关于 ajax 发送失败原因:* vue项目中,vue.config.js 或者 vite.config.js 配置了代理,这个代理在开发环境中可以用,是由脚手架提供的,* 但是打包完了以后,打包出来的是 html、css、js 纯文件,没有脚手架了,也就没有代理服务器对接口进行转发了,* 那 ajax 访问的地址也就是错误的*/
app.use('/dev',createProxyMiddleware({target: 'http://sph-api.atguigu.cn',changeOrigin: true,pathRewrite: { '^/dev' : '' }})
)app.listen(3000, () => {console.log('服务启动成功。。。')
})

将打包出来的文件放到 node 环境下,指定的静态资源目录下,访问的时候,出现两个问题:

1、页面刷新 404

解决办法:
1)配置 vue 路由模式,将 history 模式改为 hash 模式,但是这种模式,地址栏比较丑,一般网站不会使用这种模式
2)node 环境修改,使用

 app.get('*', (req, res) => {res.sendFile(__dirname + '/public/index.html')})

3)node 环境修改 - 使用 connect-history-api-fallback 中间件

const history = require('connect-history-api-fallback')
app.use(history())
2、ajax 请求失效了

出现这种情况的原因是:开发环境中,由脚手架提供了代理服务器,由代理服务器将接口进行转发。
而在生产环境中,使用的是打包出来的 html、css、js 这种纯文件,没有脚手架了,也就没有代理服务器对接口进行转发了,所以 ajax 访问的地址是错误的了。

vite.config.js

server: {proxy: {[env.VITE_APP_BASE_API]: {// 获取数据的服务器地址target: env.VITE_SERVE,// 是否允许跨域changeOrigin: true,// 路径重写rewrite: (path) => path.replace(/^\/api/, '')},}},

解决办法:
1)使用 http-proxy-middleware 中间件
service.js

const { createProxyMiddleware } = require('http-proxy-middleware')
app.use('/dev',createProxyMiddleware({target: 'http://sph-api.atguigu.cn',changeOrigin: true,pathRewrite: { '^/dev' : '' }})
)

如何解决?

2、nginx 服务器部署

  • 将打包出来的文件,放到对应的目录
  • 修改 nginx.config 文件
    nginx.config
...
location / {root	D:\dist		# root 指向打包的文件所在的目录index	index.html 	index.htm	# 默认访问的文件try_files	$uri/ $url/ /index.html;	# 解决刷新 404(没有办法匹配资源时,匹配 index.html)
}location /dev/ {# 设置代理目标proxy_pass	http://sph-api.atguigu.cn/;	# 解决 ajax 地址错误的问题
}

3、云服务器部署


http://www.ppmy.cn/embedded/96937.html

相关文章

如何获取android的SHA1或SHA256

在使用地图类的功能时,例如百度地图或者高德地图,会需要在开发者平台里填写SHA1或SHA256的指纹密钥,很多开发者小伙伴还不知道如何获取。当然关于如何获取android的SHA1或SHA256,网络上进行搜索已经有很多图文教程了,本…

SpringBoot 整合 RabbitMQ 实现延迟消息

一、业务场景说明 用于解决用户下单以后,订单超时如何取消订单的问题。 用户进行下单操作(会有锁定商品库存、使用优惠券、积分一系列的操作);生成订单,获取订单的id;获取到设置的订单超时时间&#xff0…

leetcode 885. Spiral Matrix III

题目链接 You start at the cell (rStart, cStart) of an rows x cols grid facing east. The northwest corner is at the first row and column in the grid, and the southeast corner is at the last row and column. You will walk in a clockwise spiral shape to visi…

优先级队列的实现

什么是优先级队列 优先级队列是一种特殊的数据结构,它类似于队列或栈,但是每个元素都关联有一个优先级或权重。在优先级队列中,元素的出队顺序不是简单地按照它们进入队列的先后顺序(先进先出,FIFO)&#…

【安卓】多线程编程

文章目录 线程的简单应用解析异步消息处理机制使用AsyncTask 线程的简单应用 新建一个AndroidThreadTest项目&#xff0c;然后修改activity_main.xml中的代码。 <RelativeLayout xmlns:android"http://schemas.android.com/apk/res/android"android:layout_width…

Pytorch:加载断点(pth)权重参数

一、保存的模型参数及权重 #保存模型 torch.save(model_object,resnet.pth) #加载模型 modeltorch.load(resnet.pth)二、仅保存模型的权重 torch.save(my_resnet.state_dict(),"resnet.pth")resnet_model.load_state_dict(torch.load("resnet.pth"))三、仅…

ilo地址是什么

ilo地址是什么&#xff1f; iLO 地址一般是服务器的专用网络接口的 IP 地址&#xff0c;用于在服务器本地控制台不可用时对服务器进行远程管理和监控&#xff0c;例如进行远程开机、关机、安装操作系统、查看硬件状态等操作。 要获取 iLO 地址&#xff0c;通常可以在服务器的…

Redis的缓存淘汰策略

1. 查看Redis 最大的占用内存 打开redis配置文件, 设置maxmemory参数&#xff0c;maxmemory 是bytes字节类型, 注意转换 2. Redis默认内存多少可以用 注意: 在64bit系统下&#xff0c; maxmemory 设置为 0 表示不限制Redis内存使用 3. 一般生产上如何配置 一般推荐Redis 设置内…