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

View File

@ -1,10 +1,18 @@
const express = require('express'); const express = require('express');
const router = express.Router(); const router = express.Router();
const glob = require('glob'); const minify = require('express-minify');
router.use(require('./template')); router.use(require('./template'));
router.use(require('./cookie')); router.use(require('./cookie'));
router.use(require('./session')); router.use(require('./session'));
router.use(require('./session_back')); 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; module.exports = router;

View File

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

View File

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

View File

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

View File

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