js---运行dist

187 阅读1分钟

方式1: express方式,不适合history路由

/**
 * 新建根目录下(与 dist 同级) server.js
 * vue-router mode:hash
 * 启动 node server.js
 */
const express = require("express")
const app = express();
const port = 3001
// maxAge 强制缓存时间,单位是毫秒
app.use(express.static('dist', { maxAge: 1000 * 3600 }))
app.listen(port, () => {
  console.log(`访问 http:localhost:${port}`)
})

方式2:http-server方式,不适合history路由

/**
 * 
 * 全局安装 http-server
 * vue-router mode:hash
 * 在对应dist目录下的命令行执行 http-server 即可运行
 */

方式3:express方式 + connect-history-api-fallback 插件,hash 或者 history路由均可

const express = require('express');
const path = require('path');
const history = require('connect-history-api-fallback');
const app = express();
const staticFileMiddleware = express.static(path.join(__dirname + '/dist'));
app.use(staticFileMiddleware);
app.use(
 history({
   disableDotRule: true,
   verbose: true,
 }),
);
app.use(staticFileMiddleware);
app.get('/', function (req, res) {
 res.render(path.join(__dirname + '/dist/index.html'));
});
const server = app.listen(process.env.PORT || 3006, function () {
 const port = server.address().port;
 console.log(`访问 http:localhost:${port}`)
});