一 点睛
创建node 项目,并安装express,通过express快速创建web服务器,将vue打包生成dist文件夹,托管为静态资源即可,关键代码如下。
const express = require('express')
// 创建 web 服务器
const app = express()
// 托管静态资源
app.use(express.static('./dist'))
// 启动 web服务器
app.listen(80,()=>{
console.log(''web server running at http:127.0.01)
})
二 具体步骤
1 新建 vue_shop_server 项目。
2 执行如下命令,初始化包管理配置文件
F:\vue\vue_shop_server>npm init -y
3 安装 express 第三方包
F:\vue\vue_shop_server>npm i express -S
4 将 vue 项目打包生成的 dist 文件夹,拷贝到 vue_shop_server 目录下
5 新建 app.js
const express = require('express')
// 创建 web 服务器
const app = express()
// 托管静态资源
app.use(express.static('./dist'))
// 启动 web服务器
app.listen(80, () => {
console.log('web server running at http://127.0.0.1')
})
6 启动服务
F:\vue\vue_shop_server>node app.js
web server running at http://127.0.0.1
7 测试