@@ -9,8 +9,42 @@ import { PrismaClient } from '@prisma/client';
99
1010const prisma = new PrismaClient ( ) ;
1111const app = express ( ) ;
12- app . use ( cors ( ) ) ;
13- app . use ( express . json ( ) ) ;
12+
13+ // Enhanced CORS configuration
14+ app . use ( cors ( {
15+ origin : true ,
16+ credentials : true ,
17+ methods : [ 'GET' , 'POST' , 'PUT' , 'DELETE' , 'OPTIONS' ] ,
18+ allowedHeaders : [ 'Content-Type' , 'Authorization' , 'X-Requested-With' ]
19+ } ) ) ;
20+
21+ // Enhanced JSON parsing with error handling
22+ app . use ( express . json ( {
23+ limit : '10mb' ,
24+ strict : false
25+ } ) ) ;
26+
27+ app . use ( express . urlencoded ( { extended : true } ) ) ;
28+
29+ // Add request logging middleware for debugging
30+ app . use ( ( req , res , next ) => {
31+ console . log ( `${ req . method } ${ req . path } ` , {
32+ body : req . body ,
33+ headers : req . headers ,
34+ query : req . query
35+ } ) ;
36+ next ( ) ;
37+ } ) ;
38+
39+ // Error handling middleware for JSON parsing
40+ app . use ( ( ( error : any , req : express . Request , res : express . Response , next : express . NextFunction ) => {
41+ if ( error instanceof SyntaxError && 'body' in error ) {
42+ console . error ( 'JSON Parse Error:' , error . message ) ;
43+ return res . status ( 400 ) . json ( { error : 'Invalid JSON format' } ) ;
44+ }
45+ next ( ) ;
46+ } ) as express . ErrorRequestHandler ) ;
47+
1448
1549app . use ( '/api/profile' , profileRoutes )
1650app . use ( '/api/event' , eventRoutes )
@@ -20,8 +54,23 @@ app.get('/', (_req, res) => {
2054 res . json ( { message : 'Get lost' } ) ;
2155} ) ;
2256
23- // Create the serverless handler
24- export const handler = serverlessExpress ( { app } ) ;
57+ // Global error handler
58+ app . use ( ( error : any , req : express . Request , res : express . Response , next : express . NextFunction ) => {
59+ console . error ( 'Unhandled Error:' , error ) ;
60+ res . status ( 500 ) . json ( { error : 'Internal server error' , message : error . message } ) ;
61+ } ) ;
62+
63+ // Create the serverless handler with configuration
64+ export const handler = serverlessExpress ( {
65+ app,
66+ binaryMimeTypes : [
67+ 'application/octet-stream' ,
68+ 'font/*' ,
69+ 'image/*' ,
70+ 'video/*' ,
71+ 'audio/*'
72+ ]
73+ } ) ;
2574
2675async function startServer ( ) {
2776 try {
0 commit comments