11const express = require ( 'express' ) ;
2- const cors = require ( 'cors' ) ;
32const path = require ( 'path' ) ;
43
54// Import routes
@@ -9,38 +8,9 @@ const { router: saveDetailsRouter } = require('./src/npm/saveDetails');
98const { router : dataRouter } = require ( './src/npm/data' ) ;
109
1110const app = express ( ) ;
12-
13- // Custom CORS middleware
14- app . use ( ( req , res , next ) => {
15- const origin = req . get ( 'Origin' ) ;
16-
17- // Allow GET from any origin
18- if ( req . method === 'GET' ) {
19- res . setHeader ( 'Access-Control-Allow-Origin' , '*' ) ;
20- }
21- // Restrict POST and PUT to same-origin only
22- else if ( [ 'POST' , 'PUT' ] . includes ( req . method ) ) {
23- const allowedOrigin = 'http://localhost:3000' ; // Change this to your actual frontend URL
24- if ( origin === allowedOrigin || origin === 'https://scratch-id.onrender.com' ) {
25- res . setHeader ( 'Access-Control-Allow-Origin' , allowedOrigin ) ;
26- } else {
27- return res . status ( 403 ) . json ( { error : 'Forbidden: Cross-origin POST/PUT not allowed' } ) ;
28- }
29- }
30-
31- res . setHeader ( 'Access-Control-Allow-Methods' , 'GET, POST, PUT, OPTIONS' ) ;
32- res . setHeader ( 'Access-Control-Allow-Headers' , 'Content-Type, Authorization' ) ;
33-
34- if ( req . method === 'OPTIONS' ) {
35- return res . sendStatus ( 204 ) ;
36- }
37-
38- next ( ) ;
39- } ) ;
40-
4111app . use ( express . json ( ) ) ;
4212
43- // Use the imported routes
13+ // Use the imported routes (no custom CORS)
4414app . use ( route1 ) ;
4515app . use ( route2 ) ;
4616app . use ( saveDetailsRouter ) ;
0 commit comments