better view loading practice

This commit is contained in:
b1ek 2023-03-05 23:56:16 +10:00
parent 99184b9568
commit 239e90b01a
Signed by: blek
GPG Key ID: 14546221E3595D0C
2 changed files with 19 additions and 0 deletions

View File

@ -2,6 +2,7 @@ const express = require('express');
const router = express.Router();
const glob = require('glob');
router.use(require('./template'));
router.use(require('./cookie'));
router.use(require('./session'));
router.use(require('./session_back'));

18
middleware/template.js Normal file
View File

@ -0,0 +1,18 @@
const express = require('express');
const router = new express.Router();
const Helpers = require('../helpers');
router.use((err, req, res, next) => {
if (res.template) return next();
res.template = async (file, data) => {
res.send(await Helpers.ViewLoader.load(file, {
...data,
current_route: req.originalUrl,
req,
res,
require
}))
}
});
module.exports = router;