veltrixcode commited on
Commit
359755a
·
verified ·
1 Parent(s): 3940914

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +39 -2
server.js CHANGED
@@ -17,7 +17,9 @@ const server = http.createServer(app);
17
  const io = socketIo(server, {
18
  cors: {
19
  origin: "*",
20
- methods: ["GET", "POST"]
 
 
21
  }
22
  });
23
  const port = process.env.PORT || 3000;
@@ -44,10 +46,29 @@ const port = process.env.PORT || 3000;
44
  })();
45
 
46
  // Middleware
47
- app.use(cors());
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  app.use(express.json());
49
  app.use(express.static('public'));
50
 
 
 
 
51
  // Store logs in memory (in production, use a database)
52
  let serverLogs = [];
53
 
@@ -123,6 +144,22 @@ function logRequest(req, res, next) {
123
  // Apply logging middleware
124
  app.use(logRequest);
125
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  // Emit log to all connected clients
127
  function emitLog(logData) {
128
  serverLogs.unshift(logData);
 
17
  const io = socketIo(server, {
18
  cors: {
19
  origin: "*",
20
+ methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
21
+ allowedHeaders: ["Content-Type", "Authorization", "x-otp-token", "x-user-email", "x-user-id"],
22
+ credentials: true
23
  }
24
  });
25
  const port = process.env.PORT || 3000;
 
46
  })();
47
 
48
  // Middleware
49
+ app.use(cors({
50
+ origin: "*",
51
+ methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"],
52
+ allowedHeaders: [
53
+ "Content-Type",
54
+ "Authorization",
55
+ "X-Requested-With",
56
+ "Accept",
57
+ "Origin",
58
+ "x-otp-token",
59
+ "x-user-email",
60
+ "x-user-id"
61
+ ],
62
+ credentials: true,
63
+ preflightContinue: false,
64
+ optionsSuccessStatus: 204
65
+ }));
66
  app.use(express.json());
67
  app.use(express.static('public'));
68
 
69
+ // Global CORS preflight handler
70
+ app.options('*', cors());
71
+
72
  // Store logs in memory (in production, use a database)
73
  let serverLogs = [];
74
 
 
144
  // Apply logging middleware
145
  app.use(logRequest);
146
 
147
+ // CORS headers middleware for all responses
148
+ app.use((req, res, next) => {
149
+ res.header('Access-Control-Allow-Origin', '*');
150
+ res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS, PATCH');
151
+ res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With, Accept, Origin, x-otp-token, x-user-email, x-user-id');
152
+ res.header('Access-Control-Allow-Credentials', 'true');
153
+
154
+ // Handle preflight requests
155
+ if (req.method === 'OPTIONS') {
156
+ res.status(204).end();
157
+ return;
158
+ }
159
+
160
+ next();
161
+ });
162
+
163
  // Emit log to all connected clients
164
  function emitLog(logData) {
165
  serverLogs.unshift(logData);