本地怎么运行dist包

背景:最近开发的一个项目,打包部署到线上存在一些问题,但这些问题在本地又没有复现,所以需要定位修改,但每次打包给运维部署然后去验证就很废运维,所以打出dist包后先在本地运行看看效果,ok了再把dist包给运维,那问题来了,本地怎么跑dist包?

先奉上文件目录

 

 

 

1. package.json

1 {
2   "type": "module",
3   "dependencies": {
4     "express": "^4.18.1",
5     "http-proxy-middleware": "^2.0.6"
6   }
7 }

2. 执行【npm install】,然后文件夹就会生成【node_modules】文件夹

3. 创建【index.js】文件

 1 import express from "express";
 2 import path from "path"
 3 import os from "os";
 4 import proxy from'http-proxy-middleware';
 5 const getIPAddress = () => {
 6     const interfaces = os.networkInterfaces();
 7     for (const devName in interfaces) {
 8         const iFace = interfaces[devName];
 9         for (let i = 0; i < iFace.length; i++) {
10             const alias = iFace[i];
11             if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
12                 return alias.address;
13             }
14         }
15     }
16 }
17 const app = express();
18 
19 app.use('/', express.static(path.resolve(process.cwd(), './dist')));
20 
21 app.get('/', async (req, res) => {
22     res.sendFile(path.resolve(process.cwd(), './dist/index.html'))
23 })
24 // start-接口配置代理
25 app.use('/api/cockpit', proxy.createProxyMiddleware({
26   target:'https://xxxx', // 服务器api地址目录
27   changeOrigin: true,
28   pathRewrite:{
29   }
30 }));
31 // end-接口配置代理
32 app.listen(3222, () => {
33     console.log(` Your service run on:\n`,`http://localhost:${3222}\n`,`http://${getIPAddress()}:${3222}\n`);
34 })

4. 执行index.js文件,【node index.js】

 

posted @ 2022-09-09 14:31  蛙仔  阅读(1481)  评论(0编辑  收藏  举报