Skip to content

Commit

Permalink
cors for vercel
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya-Thakur committed May 31, 2021
1 parent e9d2336 commit 4518193
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
23 changes: 23 additions & 0 deletions allowCors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const allowCors = fn => async (req, res) => {
res.setHeader('Access-Control-Allow-Credentials', true)
res.setHeader('Access-Control-Allow-Origin', '*')
// another common pattern
// res.setHeader('Access-Control-Allow-Origin', req.headers.origin);
res.setHeader('Access-Control-Allow-Methods', 'GET,OPTIONS,PATCH,DELETE,POST,PUT')
res.setHeader(
'Access-Control-Allow-Headers',
'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version'
)
if (req.method === 'OPTIONS') {
res.status(200).end()
return
}
return await fn(req, res)
}

const handler = (req, res) => {
const d = new Date()
res.end(d.toString())
}

module.exports = allowCors(handler)
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ app.listen(port, () => {
console.log('Express server started at port: 3000');
});
app.use(cors());
app.use( function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept');
next();
});
// app.use( function(req, res, next) {
// res.header('Access-Control-Allow-Origin', '*');
// res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
// res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept');
// next();
// });
app.use('/user', userController);
app.use('/survey', surveyController);
app.use('/response', responseController);
Expand Down
13 changes: 12 additions & 1 deletion vercel.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,16 @@
"src": "/",
"dest": "ui/build/index.html"
}
]
],
"headers": [
{
"source": "/(.*)",
"headers": [
{ "key": "Access-Control-Allow-Credentials", "value": "true" },
{ "key": "Access-Control-Allow-Origin", "value": "*" },
{ "key": "Access-Control-Allow-Methods", "value": "GET,OPTIONS,PATCH,DELETE,POST,PUT" },
{ "key": "Access-Control-Allow-Headers", "value": "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version" }
]
}
]
}

1 comment on commit 4518193

@vercel
Copy link

@vercel vercel bot commented on 4518193 May 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deployment failed with the following error:

If `rewrites`, `redirects`, `headers`, `cleanUrls` or `trailingSlash` are used, then `routes` cannot be present.

Learn More: https://vercel.link/mix-routing-props

Please sign in to comment.