working ! testing !
All checks were successful
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Successful in 5m31s
All checks were successful
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Successful in 5m31s
This commit is contained in:
21
server.ts
21
server.ts
@@ -1,5 +1,6 @@
|
||||
// server.ts
|
||||
import express, { Request, Response, NextFunction } from 'express';
|
||||
import timeout from 'connect-timeout';
|
||||
import cookieParser from 'cookie-parser';
|
||||
import listEndpoints from 'express-list-endpoints';
|
||||
import { getPool } from './src/services/db/connection';
|
||||
@@ -53,9 +54,11 @@ app.use(express.urlencoded({ limit: '100mb', extended: true }));
|
||||
app.use(cookieParser()); // Middleware to parse cookies
|
||||
app.use(passport.initialize()); // Initialize Passport
|
||||
|
||||
// Add a request timeout middleware. This will help prevent requests from hanging indefinitely.
|
||||
// We set a generous 5-minute timeout to accommodate slow AI processing for large flyers.
|
||||
app.use(timeout('5m'));
|
||||
|
||||
// --- Logging Middleware ---
|
||||
|
||||
const getDurationInMilliseconds = (start: [number, number]): number => {
|
||||
const NS_PER_SEC = 1e9;
|
||||
const NS_TO_MS = 1e6;
|
||||
@@ -66,6 +69,12 @@ const getDurationInMilliseconds = (start: [number, number]): number => {
|
||||
const requestLogger = (req: Request, res: Response, next: NextFunction) => {
|
||||
const start = process.hrtime();
|
||||
const { method, originalUrl } = req;
|
||||
|
||||
// If the request times out, log it.
|
||||
if (req.timedout) {
|
||||
logger.error(`REQUEST TIMEOUT: ${method} ${originalUrl} exceeded the 5m limit.`);
|
||||
}
|
||||
|
||||
logger.debug(`[Request Logger] INCOMING: ${method} ${originalUrl}`);
|
||||
|
||||
res.on('finish', () => {
|
||||
@@ -112,10 +121,16 @@ app.use('/api/users', userRouter);
|
||||
|
||||
// Basic error handling middleware
|
||||
app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
|
||||
logger.error('Unhandled application error:', { error: err.stack });
|
||||
// Check if the error is from the timeout middleware
|
||||
if (req.timedout) {
|
||||
// The timeout event is already logged by the requestLogger, but we can add more detail here if needed.
|
||||
// The response is handled by the timeout middleware itself, so we don't send another one.
|
||||
return;
|
||||
}
|
||||
logger.error('Unhandled application error:', { error: err.stack, path: req.originalUrl });
|
||||
// The 'next' parameter is required for Express to identify this as an error-handling middleware.
|
||||
// We log it here to satisfy the 'no-unused-vars' lint rule, as it's not called in this terminal handler.
|
||||
logger.info('Terminal error handler invoked. The "next" function is part of the required signature.', { next: String(next) });
|
||||
// logger.info('Terminal error handler invoked. The "next" function is part of the required signature.', { next: String(next) });
|
||||
if (!res.headersSent) {
|
||||
res.status(500).json({ message: 'Something broke!' });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user