方式1: express方式,不适合history路由
const express = require("express")
const app = express();
const port = 3001
app.use(express.static('dist', { maxAge: 1000 * 3600 }))
app.listen(port, () => {
console.log(`访问 http:localhost:${port}`)
})
方式2:http-server方式,不适合history路由
方式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}`)
});