Remove compression from Express, add CacheHeaders middleware.

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2024-04-23 20:47:47 +01:00
parent 90500b88af
commit c698187cdf
3 changed files with 21 additions and 3 deletions

9
app.js
View File

@ -1,5 +1,7 @@
// OwlBoard - © Fred Boniface 2022-2023 - Licensed under GPLv3 (or later) // OwlBoard - © Fred Boniface 2022-2023 - Licensed under GPLv3 (or later)
import { setCacheHeaders } from "./src/middlewares/setHeaders.middlewares";
// Please see the included LICENSE file // Please see the included LICENSE file
const mode = process.env.NODE_ENV || "development"; const mode = process.env.NODE_ENV || "development";
@ -13,7 +15,6 @@ const express = require("express");
const app = express(); const app = express();
// Middleware // Middleware
const compression = require("compression");
const rateLimit = require("express-rate-limit"); const rateLimit = require("express-rate-limit");
const cors = require("cors"); const cors = require("cors");
const authenticate = require("./src/middlewares/auth.middlewares"); const authenticate = require("./src/middlewares/auth.middlewares");
@ -52,14 +53,13 @@ app.use((err, req, res, next) => {
return; return;
}); });
// Global Middleware: // Pre Middleware:
app.use( app.use(
cors({ cors({
origin: "*", //[/\.owlboard\.info$/, 'localhost:5173', 'localhost:4173'] origin: "*", //[/\.owlboard\.info$/, 'localhost:5173', 'localhost:4173']
}) })
); );
app.use(express.json()); //JSON Parsing for POST Requests app.use(express.json()); //JSON Parsing for POST Requests
app.use(compression()); // Compress API Data if supported by client
//app.use(limiter); //app.use(limiter);
app.use(authenticate); app.use(authenticate);
@ -78,6 +78,9 @@ app.use("/api/v1/auth/test", authenticate, (req, res) =>
}) })
); // Returns 401 if auth failed, 200 if successful. ); // Returns 401 if auth failed, 200 if successful.
// Post middleware
app.use(setCacheHeaders)
// Number of proxies: // Number of proxies:
app.set("trust proxy", 4); app.set("trust proxy", 4);
mode === "development" mode === "development"

View File

@ -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();
}

View File

@ -8,5 +8,10 @@ declare global {
export interface Request { export interface Request {
isAuthed: boolean; isAuthed: boolean;
} }
export interface Response {
cacheType: string;
cacheSecs: number;
}
} }
} }