Skip to content

Commit c6d4dda

Browse files
authored
Update server.js
1 parent c7249a0 commit c6d4dda

File tree

1 file changed

+1
-31
lines changed

1 file changed

+1
-31
lines changed

server.js

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const express = require('express');
2-
const cors = require('cors');
32
const path = require('path');
43

54
// Import routes
@@ -9,38 +8,9 @@ const { router: saveDetailsRouter } = require('./src/npm/saveDetails');
98
const { router: dataRouter } = require('./src/npm/data');
109

1110
const 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-
4111
app.use(express.json());
4212

43-
// Use the imported routes
13+
// Use the imported routes (no custom CORS)
4414
app.use(route1);
4515
app.use(route2);
4616
app.use(saveDetailsRouter);

0 commit comments

Comments
 (0)