Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
H
Hire-Guru
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Rishikumar
Hire-Guru
Commits
80147bab
Commit
80147bab
authored
Apr 08, 2026
by
AravindR-K
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: harden CORS to allow Vercel deployments
parent
f446c644
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
10 deletions
+25
-10
Backend/server.js
Backend/server.js
+25
-10
No files found.
Backend/server.js
View file @
80147bab
...
@@ -13,21 +13,36 @@ connectDB();
...
@@ -13,21 +13,36 @@ connectDB();
const
app
=
express
();
const
app
=
express
();
// Middleware
// Middleware
app
.
use
(
cors
({
const
allowedOrigins
=
[
origin
:
function
(
origin
,
callback
)
{
const
allowed
=
[
'
http://localhost:4200
'
,
'
http://localhost:4200
'
,
'
http://localhost:3000
'
,
process
.
env
.
FRONTEND_URL
process
.
env
.
FRONTEND_URL
];
].
filter
(
Boolean
);
// Allow any origin that ends with 'vercel.app' or is directly allowed
if
(
!
origin
||
allowed
.
includes
(
origin
)
||
origin
.
endsWith
(
'
vercel.app
'
))
{
app
.
use
(
cors
({
callback
(
null
,
true
);
origin
:
function
(
origin
,
callback
)
{
}
else
{
// Allow requests with no origin (mobile apps, curl, Postman, server-to-server)
callback
(
null
,
false
);
if
(
!
origin
)
return
callback
(
null
,
true
);
// Allow any vercel.app previews and production deployments
if
(
origin
.
endsWith
(
'
.vercel.app
'
)
||
origin
.
includes
(
'
vercel.app
'
))
{
return
callback
(
null
,
true
);
}
// Allow explicitly listed origins
if
(
allowedOrigins
.
includes
(
origin
))
{
return
callback
(
null
,
true
);
}
}
// Block everything else
console
.
warn
(
`CORS blocked origin:
${
origin
}
`
);
return
callback
(
new
Error
(
`CORS policy: origin
${
origin
}
not allowed`
),
false
);
},
},
credentials
:
true
credentials
:
true
,
methods
:
[
'
GET
'
,
'
POST
'
,
'
PUT
'
,
'
DELETE
'
,
'
OPTIONS
'
],
allowedHeaders
:
[
'
Content-Type
'
,
'
Authorization
'
]
}));
}));
app
.
use
(
express
.
json
());
app
.
use
(
express
.
json
());
app
.
use
(
express
.
urlencoded
({
extended
:
true
}));
app
.
use
(
express
.
urlencoded
({
extended
:
true
}));
app
.
use
(
cookieParser
());
app
.
use
(
cookieParser
());
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment