diff --git a/app.js b/app.js index 2aa3946..8e38ac3 100644 --- a/app.js +++ b/app.js @@ -1,5 +1,7 @@ // OwlBoard - © Fred Boniface 2022-2023 - Licensed under GPLv3 (or later) +import { setCacheHeaders } from "./src/middlewares/setHeaders.middlewares"; + // Please see the included LICENSE file const mode = process.env.NODE_ENV || "development"; @@ -13,7 +15,6 @@ const express = require("express"); const app = express(); // Middleware -const compression = require("compression"); const rateLimit = require("express-rate-limit"); const cors = require("cors"); const authenticate = require("./src/middlewares/auth.middlewares"); @@ -52,14 +53,13 @@ app.use((err, req, res, next) => { return; }); -// Global Middleware: +// Pre Middleware: app.use( cors({ origin: "*", //[/\.owlboard\.info$/, 'localhost:5173', 'localhost:4173'] }) ); app.use(express.json()); //JSON Parsing for POST Requests -app.use(compression()); // Compress API Data if supported by client //app.use(limiter); app.use(authenticate); @@ -78,6 +78,9 @@ app.use("/api/v1/auth/test", authenticate, (req, res) => }) ); // Returns 401 if auth failed, 200 if successful. +// Post middleware +app.use(setCacheHeaders) + // Number of proxies: app.set("trust proxy", 4); mode === "development" diff --git a/src/middlewares/setHeaders.middlewares.ts b/src/middlewares/setHeaders.middlewares.ts new file mode 100644 index 0000000..e0098f0 --- /dev/null +++ b/src/middlewares/setHeaders.middlewares.ts @@ -0,0 +1,10 @@ +import { NextFunction, Request, Response } from "express"; + +export function setCacheHeaders(req: Request, res: Response, next: NextFunction) { + if (res.cacheType && res.cacheSecs) { + const cacheCtlHeader = `${res.cacheType}, max-age=${res.cacheSecs}`; + res.setHeader('Cache-Control', cacheCtlHeader) + } + + res.send(); +} \ No newline at end of file diff --git a/src/types/index.d.ts b/src/types/index.d.ts index 48fb262..795aa74 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -8,5 +8,10 @@ declare global { export interface Request { isAuthed: boolean; } + + export interface Response { + cacheType: string; + cacheSecs: number; + } } }