const express = require('express');
const proxy = require('http-proxy-middleware');
// 服务端压缩gzip
// var compression = require('compression');
// webpack打包好gzip文件
const expressStaticGzip = require('express-static-gzip');
const app = express();
// 启用服务端压缩gzip
// app.use(compression());
app.use(
expressStaticGzip('../deploy', {
maxAge: '3d',
setHeaders: setCustomCacheControl,
})
);
function setCustomCacheControl(res, currentFilePath, stat) {
if (currentFilePath.match(/\index\.html$/)) {
// Custom Cache-Control for HTML files
res.setHeader('Cache-Control', 'no-cache');
}
}
app.use(express.static('../deploy'));
const PROXY_EDSP = process.env.PROXY_API || 'http://10.22.0.137:31801/edsp';
app.use(
'/api',
proxy({
target: PROXY_EDSP,
pathRewrite: {
'/api': '',
},
changeOrigin: true,
}),
);
app.listen(9900, (req, res) => {
console.log(req, res);
console.log('启动成功,请通过localhost:9900访问');
});
【前端部署】Node+Express部署前端(gzip资源压缩/转发,proxy代理)
最新推荐文章于 2024-02-19 17:11:57 发布