Express static does not set correct header so setting manual

This commit is contained in:
Fabian Stamm 2020-04-03 16:30:49 +02:00
parent f9083a1d27
commit be8a2f7b2c

View File

@ -1,4 +1,10 @@
import { IRouter, Request, Router, static as ServeStatic } from "express"; import {
IRouter,
Request,
Router,
static as ServeStatic,
RequestHandler,
} from "express";
import * as Handlebars from "handlebars"; import * as Handlebars from "handlebars";
import * as moment from "moment"; import * as moment from "moment";
import { GetUserMiddleware, UserMiddleware } from "../api/middlewares/user"; import { GetUserMiddleware, UserMiddleware } from "../api/middlewares/user";
@ -12,9 +18,14 @@ import GetRegistrationPage from "./register";
Handlebars.registerHelper("appname", () => config.core.name); Handlebars.registerHelper("appname", () => config.core.name);
const cacheTime = config.core.dev const cacheTime = config.core.dev
? moment.duration(1, "month").asMilliseconds() ? moment.duration(1, "month").asSeconds()
: 1000; : 1000;
const addCache: RequestHandler = (req, res, next) => {
res.setHeader("cache-control", "public, max-age=" + cacheTime);
next();
};
const ViewRouter: IRouter = Router(); const ViewRouter: IRouter = Router();
ViewRouter.get("/", UserMiddleware, (req, res) => { ViewRouter.get("/", UserMiddleware, (req, res) => {
res.send("This is the main page"); res.send("This is the main page");
@ -27,12 +38,14 @@ ViewRouter.get("/register", (req, res) => {
ViewRouter.use( ViewRouter.use(
"/login", "/login",
ServeStatic("./views_repo/build/login", { maxAge: cacheTime }) addCache,
ServeStatic("./views_repo/build/login", { cacheControl: false })
); );
ViewRouter.use( ViewRouter.use(
"/user", "/user",
ServeStatic("./views_repo/build/user", { maxAge: cacheTime }) addCache,
ServeStatic("./views_repo/build/user", { cacheControl: false })
); );
ViewRouter.get("/code", (req, res) => { ViewRouter.get("/code", (req, res) => {