diff --git a/helpers/index.js b/helpers/index.js index b1d25d4..7ee93fd 100644 --- a/helpers/index.js +++ b/helpers/index.js @@ -1,6 +1,7 @@ module.exports = { - ViewLoader: require('./view_loader'), - TimeSince: require('./timesince'), - HtmlString: require('./htmlstring'), - GPG: require('./gpg') + ViewLoader: require('./view_loader'), + TimeSince: require('./timesince'), + HtmlString: require('./htmlstring'), + GPG: require('./gpg'), + Minify: require('./minify') } \ No newline at end of file diff --git a/helpers/minify.js b/helpers/minify.js new file mode 100644 index 0000000..66884c7 --- /dev/null +++ b/helpers/minify.js @@ -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 }; \ No newline at end of file diff --git a/index.js b/index.js index ad4e255..e8e33fb 100644 --- a/index.js +++ b/index.js @@ -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', { - error: '404 Not Found', - message: 'The requested page was not found.' - })) - } catch (err) { - next(err); - } + if (res.headersSent) return next(); + + res.status(404); + res.template('error.pug', { + error: '404 Not Found', + message: 'The requested page was not found.' + }); }) - - // 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, () => { diff --git a/middleware/index.js b/middleware/index.js index 547dcc7..8522718 100644 --- a/middleware/index.js +++ b/middleware/index.js @@ -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; \ No newline at end of file diff --git a/middleware/template.js b/middleware/template.js index 5865962..fd0e47c 100644 --- a/middleware/template.js +++ b/middleware/template.js @@ -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; \ No newline at end of file + + return next(); +} \ No newline at end of file diff --git a/package.json b/package.json index f50299d..1556335 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/routes/homepage.js b/routes/homepage.js index f01efc5..7ea71c3 100644 --- a/routes/homepage.js +++ b/routes/homepage.js @@ -13,21 +13,19 @@ async function handler(req, res) { limit: 5 }); - res.send( - await Helpers.ViewLoader.load( - 'main.pug', - { - current_route: '/', - gb_entries, - articles, - og: { - title: 'blek! Site', - type: 'website', - image: req.protocol + '://' + req.get('host') + '/content/transylveonia.jpg', - url: req.protocol + '://' + req.get('host') + req.originalUrl - } + await res.template( + 'main.pug', + { + current_route: '/', + gb_entries, + articles, + og: { + title: 'blek! Site', + type: 'website', + image: req.protocol + '://' + req.get('host') + '/content/transylveonia.jpg', + url: req.protocol + '://' + req.get('host') + req.originalUrl } - ) + } ); return; } diff --git a/startup.js b/startup.js index f57d855..84f67ca 100644 --- a/startup.js +++ b/startup.js @@ -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 = '*/*'; }