add minify middleware

This commit is contained in:
b1ek 2023-03-06 00:26:56 +10:00
parent 239e90b01a
commit a6702f4722
Signed by: blek
GPG Key ID: 14546221E3595D0C
8 changed files with 62 additions and 40 deletions

View File

@ -2,5 +2,6 @@ module.exports = {
ViewLoader: require('./view_loader'),
TimeSince: require('./timesince'),
HtmlString: require('./htmlstring'),
GPG: require('./gpg')
GPG: require('./gpg'),
Minify: require('./minify')
}

16
helpers/minify.js Normal file
View File

@ -0,0 +1,16 @@
const minify = require('@node-minify/core');
const cleanCSS = require('@node-minify/clean-css');
/**
*
* @param {string} content
* @returns {string}
*/
async function css(content) {
return await minify({
compressor: cleanCSS,
content
});
}
module.exports = { css };

View File

@ -24,20 +24,15 @@ app.use(express.static('public'));
// 404
app.use(async (req, res, next) => {
try {
if (res.headersSent) return next();
const Helpers = require('./helpers');
res.status(404).send(await Helpers.ViewLoader.load('error.pug', {
res.status(404);
res.template('error.pug', {
error: '404 Not Found',
message: 'The requested page was not found.'
}))
} catch (err) {
next(err);
}
});
})
// error handler
app.use(async (err, req, res, next) => {
console.log(err);
@ -45,13 +40,11 @@ app.use(async (err, req, res, next) => {
return next(err);
}
const Helpers = require('./helpers');
res.status(500);
res.send(await Helpers.ViewLoader.load('error.pug', {
res.template('error.pug', {
error: '500 Internal Server Error',
message: 'An unexpected error happened in the server'
}));
});
})
const server = app.listen(APP_PORT, () => {

View File

@ -1,10 +1,18 @@
const express = require('express');
const router = express.Router();
const glob = require('glob');
const minify = require('express-minify');
router.use(require('./template'));
router.use(require('./cookie'));
router.use(require('./session'));
router.use(require('./session_back'));
router.use(minify({
cache: process.env.APP_DEBUG == true,
uglifyJS: null,
errorHandler: console.error,
css_match: /.css^/,
js_match: /.js^/
}));
module.exports = router;

View File

@ -2,8 +2,10 @@ const express = require('express');
const router = new express.Router();
const Helpers = require('../helpers');
router.use((err, req, res, next) => {
module.exports = (req, res, next) => {
if (res.template) return next();
res.template = async (file, data) => {
res.send(await Helpers.ViewLoader.load(file, {
...data,
@ -13,6 +15,6 @@ router.use((err, req, res, next) => {
require
}))
}
});
module.exports = router;
return next();
}

View File

@ -12,6 +12,8 @@
"author": "",
"license": "ISC",
"dependencies": {
"@node-minify/clean-css": "^8.0.5",
"@node-minify/core": "^8.0.5",
"bcrypt": "^5.1.0",
"body-parser": "^1.20.1",
"connect-redis": "^6.1.3",
@ -21,6 +23,7 @@
"dotenv": "^16.0.3",
"express": "^4.18.2",
"express-async-handler": "^1.2.0",
"express-minify": "^1.0.0",
"express-session": "^1.17.3",
"glob": "^8.1.0",
"gulp": "^4.0.2",

View File

@ -13,8 +13,7 @@ async function handler(req, res) {
limit: 5
});
res.send(
await Helpers.ViewLoader.load(
await res.template(
'main.pug',
{
current_route: '/',
@ -27,7 +26,6 @@ async function handler(req, res) {
url: req.protocol + '://' + req.get('host') + req.originalUrl
}
}
)
);
return;
}

View File

@ -18,7 +18,8 @@ require('dotenv').config({
});
// load debug
if (process.env.APP_DEBUG) {
if (process.env.APP_DEBUG == 'true') {
process.env.APP_DEBUG = true;
process.env.DEBUG = '*/*';
}