title: Enable Cors for Firebase Cloud Functions
body: <p>I am trying to enable <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS" rel="noreferrer">cors</a> for my http Cloud Functions. However, even when following <a href="https://github.com/firebase/functions-samples/blob/master/quickstarts/time-server/functions/index.js" rel="noreferrer">this example that uses cors</a>, when trying to <a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API" rel="noreferrer"><code>fetch</code></a>, it still gives me the error:</p>
  
  <blockquote>
    <p>Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.</p>
  </blockquote>
  
  <p>Here is my cloud function:</p>
  
  <pre class="lang-js prettyprint-override"><code>const functions = require('firebase-functions');
  const cors = require('cors')({origin: true});
  
  exports.foo = functions.https.onRequest((req, res) =&gt; {
      return cors(req, res, () =&gt; {
          let format = req.query.format;
          if (!format) {
              format = req.body.format;
          }
  
          //do stuff
  
      });
  });
  </code></pre>
  
  <p>And here is how I fetch:</p>
  
  <pre><code>fetch("https://us-central1-my-database.cloudfunctions.net/foo", {
      method: "POST", 
      body: JSON.stringify(data),
      headers: {
        "Content-Type": "application/json"
      }
  }).then((res) =&gt; {
      // ...
  }).catch((err) =&gt; {
      // ...
  });
  </code></pre>
  
  <p>Why isn't cors working, and how can I fix it?</p>