Remove compression from Express, add CacheHeaders middleware.
Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
parent
90500b88af
commit
c698187cdf
9
app.js
9
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"
|
||||
|
10
src/middlewares/setHeaders.middlewares.ts
Normal file
10
src/middlewares/setHeaders.middlewares.ts
Normal 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();
|
||||
}
|
5
src/types/index.d.ts
vendored
5
src/types/index.d.ts
vendored
@ -8,5 +8,10 @@ declare global {
|
||||
export interface Request {
|
||||
isAuthed: boolean;
|
||||
}
|
||||
|
||||
export interface Response {
|
||||
cacheType: string;
|
||||
cacheSecs: number;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user