npm-package-nodejs-utils-lda
Version:
Este projeto tem como fins de criar e abstrair módulos basicos e utilidades para o node js
47 lines (40 loc) • 1.28 kB
JavaScript
import { fopen, fwrite, log } from "./autoFileSysModule.mjs";
import { configExist } from "./utils.mjs";
import { config } from "dotenv";
const logPath = "cache.txt";
config();
// Carregar variáveis
configExist();
checkConfigIntegrity();
function setCacheHeaders(req, res, next) {
const configs = fopen("config.json");
if (req.headers["x-disable-cache"] === "true") {
log(
`🚫 Disabling cache for this request. ${req.method}:${req.path}`,
logPath
);
res.set("Cache-Control", "no-store");
return next();
}
// Cache de 1 hora
const cacheTime = 60 * configs.cacheDurationInMinutes; // 1 hora em segundos
log(
`✅ Adding ${cacheTime} minutes Cache header. ${req.method}:${req.path}`,
logPath
);
res.set("Cache-Control", `public, max-age=${cacheTime}`);
res.set("Cache-time", cacheTime);
next();
}
function checkConfigIntegrity() {
// obtem config.json
const configs = fopen("config.json");
// verifica se blockedRoutes não existe
if (!configs.cacheDurationInMinutes) {
// caso não exista configura para uma rota padrão
configs.cacheDurationInMinutes = 30;
// salva novamente
fwrite("config.json", configs);
}
}
export default setCacheHeaders;