网站首页 > 精选文章 正文
1. 学习目标
今天我们带大家学习一下如何打包和部署项目。
2. npm run build 项目打包
在项目编写完成之后,我们在项目的根目录下运行以下命令:
npm run build
打包结束之后,我们可以看到项目目录里面多了一个 dist 文件夹,这个文件夹里面就是我们项目打包之后的代码。
3. Node 服务部署
我们在项目的根目录下创建 server.js,用来启动 vue 项目:
const fs = require("fs");
const path = require("path");
const bodyParser = require("body-parser");
const express = require("express");
const app = express();
const list = require("./mock/list.json");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false })); // 服务开启后访问指定编译好的dist文件下的数据
app.use(express.static(path.resolve(__dirname, "./dist")));
app.get("/todo/list", (req, res) => {
res.json({
data: list
});
});
app.get("*", function(req, res) {
const html = fs.readFileSync(
path.resolve(__dirname, "./dist/index.html"),
"utf-8"
);
res.send(html);
});
app.listen(8081);
运行命令:
node start.js
然后,访问 http://localhost:8081/#/ 就可以正常预览项目了。
4. 小结
本节我们带大家学习了项目的打包部署,主要有以下知识点:
- 利用 npm run build 打包项目。
- 利用 node 服务部署打包后的项目。
猜你喜欢
- 2024-12-27 使用 nginx 同域名下部署多个 vue 项目,并使用反向代理
- 2024-12-27 vue3+electron开发桌面软件(5)——系统级右键实现文件上传
- 2024-12-27 Vue:npm run serve 到底做了什么?
- 2024-12-27 深入探索Vue3:将您的项目打包为生产版本
- 2024-12-27 Quasar 火了!为何能成为 Vue 顶级开发框架?
- 2024-12-27 在vue开发中如何利用.env文件
- 2024-12-27 nginx 部署vue项目
- 2024-12-27 如何将Vue的前端项目直接集成到SpringBoot项目的Jar包中?
- 2024-12-27 Vite是什么?在Vue3中如何使用Vite?
- 2024-12-27 手把手教你用 Tauri+Vue 创建小型桌面应用
- 最近发表
- 标签列表
-
- 向日葵无法连接服务器 (32)
- git.exe (33)
- vscode更新 (34)
- dev c (33)
- git ignore命令 (32)
- gitlab提交代码步骤 (37)
- java update (36)
- vue debug (34)
- vue blur (32)
- vscode导入vue项目 (33)
- vue chart (32)
- vue cms (32)
- 大雅数据库 (34)
- 技术迭代 (37)
- 同一局域网 (33)
- github拒绝连接 (33)
- vscode php插件 (32)
- vue注释快捷键 (32)
- linux ssr (33)
- 微端服务器 (35)
- 导航猫 (32)
- 获取当前时间年月日 (33)
- stp软件 (33)
- http下载文件 (33)
- linux bt下载 (33)