Skip to content

Commit

Permalink
[Task] #7 add basic cache to express
Browse files Browse the repository at this point in the history
  • Loading branch information
Type-Style committed Jan 21, 2024
1 parent 57fbd75 commit a28f896
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ require('module-alias/register');
import { config } from 'dotenv';
import express from 'express';
import hpp from 'hpp';
import cache from './cache';
import writeRouter from '@src/controller/write';
import path from 'path';
import logger from '@src/scripts/logger';


// configurations
config();
const app = express();
app.use(hpp());
app.use(cache);

// routes
app.get('/', (req, res) => {
Expand Down
15 changes: 15 additions & 0 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {Request, Response, NextFunction } from 'express';

const setCache = function (req: Request, res: Response, next: NextFunction) {
const seconds = 60 * 5; // 5 minuits

// cache get requests but nothing else
if (req.method == "GET") {
res.set("Cache-control", `public, max-age=${seconds}`);
} else {
res.set("Cache-control", 'no-store');
}

next ();
}
export default setCache;

0 comments on commit a28f896

Please sign in to comment.