代码拉取完成,页面将自动刷新
同步操作将从 Sam-meng/vue-cli 高级模板 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
/**
* 静态资源服务 (node 运行)
* 通常用于预览/检查打包结果,或者临时给其他人员启用前端服务
*/
const express = require('express')
const compression = require('compression')
const { createProxyMiddleware } = require('http-proxy-middleware')
const { join } = require('path')
const os = require('os')
const open = require('open')
const BASE_URL = '' // 打包时的 process.env.BASE_URL
const port = 8181
const isHistoryMode = false
const projectDir = join(__dirname, './dist')
const app = express()
/* 代理,更详细的配置规则:https://github.com/chimurai/http-proxy-middleware#options */
app.use(
'/api',
createProxyMiddleware({
target: 'http://10.25.73.159:8081',
pathRewrite: { '^/api': '' },
}),
)
/* 静态资源 */
app.use(compression())
app.use(BASE_URL, function(req, res) {
let sendfilePath = req.path
let cacheControl = 'no-cache'
const isStatic = /\.\w+$/.test(req.path)
const isStaticHtmlEntry = isStatic && /^\/index\.html/.test(req.path)
const isStaticHashCache = isStatic && /^\/static-hash\//.test(req.path)
if (isStatic) {
if (isStaticHtmlEntry) {
cacheControl = 'no-store'
} else if (isStaticHashCache) {
cacheControl = 'public,max-age=31536000'
}
} else {
if (isHistoryMode) {
cacheControl = 'no-store'
sendfilePath = '/index.html'
} else {
if (req.path === '/') {
cacheControl = 'no-store'
}
sendfilePath = join(req.path, '/index.html')
}
}
res.setHeader('Cache-Control', cacheControl)
res.sendfile(
join(projectDir, sendfilePath),
err => err && res.status(err.status).send(err.status),
)
})
app.use((req, res) => res.status(404).send(404))
/* 启动 */
app.listen(port, function() {
const ip = (() => {
const interfaces = os.networkInterfaces()
for (const devName in interfaces) {
const iface = interfaces[devName]
for (let i = 0; i < iface.length; i++) {
const alias = iface[i]
if (
alias.family === 'IPv4' &&
alias.address !== '127.0.0.1' &&
!alias.internal
) {
return alias.address
}
}
}
})()
const local1 = `http://localhost:${port}${BASE_URL}`
const local2 = `http://127.0.0.1:${port}${BASE_URL}`
const network = `http://${ip || '未连接网络'}:${port}${BASE_URL}`
global.console.log('')
global.console.log(` Local1: ${local1}`)
global.console.log(` Local2: ${local2}`)
global.console.log(`Network: ${network}`)
global.console.log('')
open(ip ? network : local2)
})
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。