Skip to content

Commit

Permalink
try out not caching post requests
Browse files Browse the repository at this point in the history
  • Loading branch information
ebubae committed Nov 7, 2024
1 parent be3a191 commit b2e35e3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import './shared/utils/wrapConsole';

const app = express();

app.set('view engine', 'jade');
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ extended: false, limit: '50mb' }));
// app.set('view engine', 'jade');
app.use(express.json());
// app.use(express.json({ limit: '50mb' }));
// app.use(express.urlencoded({ extended: false, limit: '50mb' }));
// app.use(compression());

if (process.env.NODE_ENV === 'development') {
Expand Down
7 changes: 5 additions & 2 deletions src/middleware/cache.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { NextFunction, Request, Response } from 'express';

export default (maxAge = 302400, smaxAge = 604800) =>
export default (maxAge = 302400, smaxAge = 604800, noCache = false) =>
(req: Request, res: Response, next: NextFunction) => {
res.set('Cache-Control', `public, max-age=${maxAge}, s-maxage=${smaxAge}`);
res.set(
'Cache-Control',
noCache ? 'no-store' : `public, max-age=${maxAge}, s-maxage=${smaxAge}`
);
return next();
};
3 changes: 2 additions & 1 deletion src/routers/routerV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import validId from '../middleware/validId';
import validateApiKey from '../middleware/validateApiKey';
import analytics from '../middleware/analytics';
import attachRedisClient from '../middleware/attachRedisClient';
import cache from 'src/middleware/cache';

const routerV2 = Router();

Expand All @@ -27,7 +28,7 @@ routerV2.get(

// Speech-to-Text
routerV2.post('/speech-to-text', analytics, validateApiKey, getTranscription);
routerV2.post('/translate', analytics, validateApiKey, getTranslation);
routerV2.post('/translate', cache(0, 0, true), analytics, validateApiKey, getTranslation);

// Redirects to V1
routerV2.post('/developers', (_, res) => res.redirect('/api/v1/developers'));
Expand Down

0 comments on commit b2e35e3

Please sign in to comment.