From 4ed0b48232f2d0fdaa1ed85f243119608d2d5caf Mon Sep 17 00:00:00 2001 From: Mark Powers Date: Tue, 23 Feb 2021 20:08:20 -0600 Subject: Add apicache to routes --- package-lock.json | 5 +++++ package.json | 1 + src/server.js | 56 +++++++++++++++++++++++++++---------------------------- 3 files changed, 34 insertions(+), 28 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8f43397..92ab58c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -49,6 +49,11 @@ "resolved": "https://registry.npmjs.org/any-promise/-/any-promise-1.3.0.tgz", "integrity": "sha1-q8av7tzqUugJzcA3au0845Y10X8=" }, + "apicache": { + "version": "1.6.2", + "resolved": "https://registry.npmjs.org/apicache/-/apicache-1.6.2.tgz", + "integrity": "sha512-3z5e+1E2qwZoqzFVgdx5l9nGhSG0kHv3v2G170vnJSz5uj4mCLVZfRw0o37aWwV8pTPXSkB8OBZz3TIur4H26g==" + }, "append-field": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/append-field/-/append-field-1.0.0.tgz", diff --git a/package.json b/package.json index 4fab4bf..a3d4527 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "author": "", "license": "ISC", "dependencies": { + "apicache": "^1.6.2", "body-parser": "^1.19.0", "cookie-parser": "^1.4.4", "express": "^4.17.1", diff --git a/src/server.js b/src/server.js index 2198923..297c135 100644 --- a/src/server.js +++ b/src/server.js @@ -7,6 +7,7 @@ const uuidv4 = require('uuid/v4'); const path = require('path'); const rss = require('rss'); const marked = require('marked'); +const cache = require('apicache').middleware; const templates = require('./templates'); @@ -155,28 +156,28 @@ function setUpRoutes(models, jwtFunctions, database, templates) { next() }) - server.get('/', async (req, res) => { + server.get('/', cache('5 minutes'), async (req, res) => { let posts = await formatPostsForType(models, "index") let body = templates["index"]({posts}); res.status(200).send(body) }) - server.get('/bread', async (req, res) => { + server.get('/bread', cache('5 minutes'), async (req, res) => { let posts = await formatPostsForType(models, "bread") let body = templates["bread"]({posts}); res.status(200).send(body) }) - server.get('/blog', async (req, res) => { + server.get('/blog', cache('5 minutes'), async (req, res) => { let posts = await formatPostsForType(models, "blog") let body = templates["blog"]({posts}); res.status(200).send(body) }) - server.get('/post/:type/:id', async (req, res) => { + server.get('/post/:type/:id', cache('5 minutes'), async (req, res) => { let posts = await formatPostsforSingle(models, req.params.type, req.params.id) let date = posts[0].createdAt; let body = templates["blog-single"]({posts, date}); res.status(200).send(body) }) - server.get('/tags/:name', async (req, res) => { + server.get('/tags/:name', cache('5 minutes'), async (req, res) => { const { name } = req.params; const postsWithTag = await models.tags.findAll({ attributes: ["postId"], where: { text: name } }) .map(function (x) { @@ -195,30 +196,29 @@ function setUpRoutes(models, jwtFunctions, database, templates) { res.status(200).send(body) }) - server.get('/admin', (req, res) => res.sendFile(__dirname + "/html/admin.html")); - server.get('/login', (req, res) => res.sendFile(__dirname + "/html/login.html")) - server.get('/email', (req, res) => res.sendFile(__dirname + "/html/email.html")) - server.get('/email-success', (req, res) => res.sendFile(__dirname + "/html/email-success.html")) - server.get('/feed', (req, res) => res.sendFile(__dirname + "/html/feed.html")); - server.get('/essay', (req, res) => res.sendFile(__dirname + "/html/essay.html")); - // server.get('/misc', (req, res) => res.sendFile(__dirname + "/html/misc.html")); - server.get('/word-square', (req, res) => res.sendFile(__dirname + "/html/word-square.html")); - server.get('/chess', (req, res) => res.sendFile(__dirname + "/html/chess.html")); - server.get('/admin/chess', async (req, res, next) => res.sendFile(__dirname + "/html/chess.html")); + server.get('/admin', cache('5 minutes'), (req, res) => res.sendFile(__dirname + "/html/admin.html")); + server.get('/login', cache('5 minutes'), (req, res) => res.sendFile(__dirname + "/html/login.html")) + server.get('/email', cache('5 minutes'), (req, res) => res.sendFile(__dirname + "/html/email.html")) + server.get('/email-success', cache('5 minutes'), (req, res) => res.sendFile(__dirname + "/html/email-success.html")) + server.get('/feed', cache('5 minutes'), (req, res) => res.sendFile(__dirname + "/html/feed.html")); + server.get('/essay', cache('5 minutes'), (req, res) => res.sendFile(__dirname + "/html/essay.html")); + server.get('/word-square', cache('5 minutes'), (req, res) => res.sendFile(__dirname + "/html/word-square.html")); + server.get('/chess', cache('5 minutes'), (req, res) => res.sendFile(__dirname + "/html/chess.html")); + server.get('/admin/chess', cache('5 minutes'), async (req, res, next) => res.sendFile(__dirname + "/html/chess.html")); // server.get('/projects', (req, res) => res.sendFile(__dirname + "/html/projects.html")); - server.get('/zines', (req, res) => res.sendFile(__dirname + "/public/zines.html")); + server.get('/zines', cache('5 minutes'), (req, res) => res.sendFile(__dirname + "/public/zines.html")); server.use('/static', express.static(__dirname + '/public')) - server.get('/misc', async (req, res) => { + server.get('/misc', cache('5 minutes'), async (req, res) => { let body = templates["misc"](); res.status(200).send(body) }) - server.get('/projects', async (req, res) => { + server.get('/projects', cache('5 minutes'), async (req, res) => { let body = templates["projects"](); res.status(200).send(body) }) - server.get('/wordsquares/best', async (req, res, next) => { + server.get('/wordsquares/best', cache('5 minutes'), async (req, res, next) => { var best = await database.query("select words, name from wordsquares where best = 1", { type: database.QueryTypes.SELECT }) res.status(200).send({ best: best }); }) @@ -252,7 +252,7 @@ function setUpRoutes(models, jwtFunctions, database, templates) { res.status(400).send(e.message); } }) - server.get('/posts/:type', async (req, res, next) => { + server.get('/posts/:type', cache('5 minutes'), async (req, res, next) => { try { const { type } = req.params; var posts = await models.posts.findAll({ @@ -266,7 +266,7 @@ function setUpRoutes(models, jwtFunctions, database, templates) { res.status(400).send(e.message); } }) - server.get('/posts/:type/:id', async (req, res, next) => { + server.get('/posts/:type/:id', cache('5 minutes'), async (req, res, next) => { try { const { type, id } = req.params; var posts = await models.posts.findAll({ @@ -354,22 +354,22 @@ function setUpRoutes(models, jwtFunctions, database, templates) { }) - server.get('/favicon.ico', (req, res) => res.sendFile(__dirname + "/icon/favicon.ico")) - server.get('/favicon.svg', (req, res) => res.sendFile(__dirname + "/icon/favicon.svg")) - server.get('/css/:id', (req, res) => { + server.get('/favicon.ico', cache('5 minutes'), (req, res) => res.sendFile(__dirname + "/icon/favicon.ico")) + server.get('/favicon.svg', cache('5 minutes'), (req, res) => res.sendFile(__dirname + "/icon/favicon.svg")) + server.get('/css/:id', cache('5 minutes'), (req, res) => { res.sendFile(__dirname + "/css/" + req.params.id); }); - server.get('/uploads/:id', (req, res) => { + server.get('/uploads/:id', cache('5 minutes'), (req, res) => { res.sendFile(__dirname + "/uploads/" + req.params.id); }); - server.get('/essay/:id', (req, res) => { + server.get('/essay/:id', cache('5 minutes'), (req, res) => { res.sendFile(__dirname + "/html/essay/" + req.params.id); }); - server.get('/js/:id', (req, res) => { + server.get('/js/:id', cache('5 minutes'), (req, res) => { res.sendFile(__dirname + "/js/" + req.params.id); }); - server.get('/feed.xml', async (req, res) => { + server.get('/feed.xml', cache('1 hour'), async (req, res) => { var feed = new rss({ title: "Mark's Kitchen", description: "Posts from marks.kitchen", -- cgit v1.2.3