Back

how do I set cors headers with node js in my cloud functions

  • 1
  • Functions
  • Web
mr.nobody4704
22 Feb, 2024, 16:30

I am trying to add custom cors headers in my cloud function because I'm not able to access it with other domains. But im not able to add them with res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Methods', 'GET, POST'); res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');

TL;DR
To set CORS headers in your Node.js cloud functions, add the headers using res.set() in your function code. Make sure the headers are set before sending the response. Example: ```javascript res.set({ 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, POST', 'Access-Control-Allow-Headers': 'Content-Type, Authorization' }); ``` Ensure the headers are set correctly and the response is sent after setting them. This should allow access from other domains.
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more