onions-node
Version:
onions-node backend
39 lines (38 loc) • 1.04 kB
JavaScript
const express = require('express')
const path = require('path')
module.exports = (distConfig, projectPath) => {
if (typeof distConfig === 'string') {
distConfig = [{
path: distConfig,
match: '/',
type: 'static',
maxage: '7d'
}]
}
const router = express.Router()
distConfig.forEach(config => {
switch (config.type) {
case 'static':
router.use(
config.match,
express.static(path.resolve(projectPath, config.path), {
setHeaders(res, path, state) {
if (/\.(html|htm)\??/.test(path)) {
res.set('Cache-Control', 'no-cache')
} else {
res.set('Cache-Control', 'public, max-age=31536000') // 1year
}
}
})
);
break
case 'html':
router.use(config.match, (req, res) => {
res.set('Cache-Control', 'no-cache');
res.sendFile(path.join(projectPath, config.path));
})
break
}
})
return router
}