先npm init,初始化package.json
随后创建项目目录如下:
|----dist
|----my-web
|----index.html
|----static
|----node_modules
|----node-server.js
|----package.json
|----package-lock.json
dist文件夹存放打包后的vue的dist,我的打包后文件为my-web
node-server.js主要写node起服务的代码
const path = require('path')
const express = require('express')
const history = require('connect-history-api-fallback')
const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express()
// 处理单页应用路由
app.use(history())
// 代理对象地址
// 配置参考vue中配置的proxy
app.use('/api', createProxyMiddleware({
target: 'https://后台域名',
changeOrigin: true,
pathRewrite: {
'^api': ''
}
}));
// 加载静态资源
app.use(express.static(path.join(__dirname, './dist')))
// 启动服务
app.listen(9548, ()=> {
console.log('success => http://localhost:9548')
})
cli操作
// 安装依赖
npm i express connect-history-api-fallback http-proxy-middleware
// 启动服务
node node-server
浏览器访问: http://localhost:9548/my-web/index.html#/login