Skip to content

Commit

Permalink
add middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
bhnuka committed Nov 10, 2024
1 parent 6f9cccb commit 0dab06d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Request, Response, NextFunction } from 'express';

export const normalizeQuestionData = (req: Request, res: Response, next: NextFunction): void => {
if (req.body) {
if (Array.isArray(req.body.categories)) {
req.body.categories = req.body.categories.map((cat: string) => cat.toLowerCase());
}
if (typeof req.body.difficulty === 'string') {
req.body.difficulty = req.body.difficulty.toLowerCase();
}
}
next();
};
6 changes: 6 additions & 0 deletions peerprep/backend/question-service/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import questionRoutes from './routes/questionRoutes';
import databaseRoutes from './routes/databaseRoutes';
import gptRoutes from './routes/gptRoutes';
import loadSampleData from './sampleData';
import { normalizeQuestionData } from './middleware/normalizationMiddleware';

connectDB() // Initialize MongoDB connection
.then(() => {
Expand Down Expand Up @@ -43,6 +44,11 @@ app.use(cors({
} as CorsOptions));
app.use(express.json());

// Apply normalization middleware to specific routes
// This middleware will normalize `categories` and `difficulty` fields to lowercase
app.use('/api/questions', normalizeQuestionData);


// API routes
app.use('/api', questionRoutes);

Expand Down

0 comments on commit 0dab06d

Please sign in to comment.