Google Cloud Functions enable CORS?

后端 未结 6 1679
暖寄归人
暖寄归人 2020-12-05 17:08

I just finished the Hello World Google Cloud Functions tutorial and received the following response headers:

Connection → keep-alive
Content-Length → 14
Cont         


        
6条回答
  •  心在旅途
    2020-12-05 17:37

    I've just created webfunc. It's a lightweight HTTP server that supports CORS as well as routing for Google Cloud Functions. Example:

    const { serveHttp, app } = require('webfunc')
    
    exports.yourapp = serveHttp([
      app.get('/', (req, res) => res.status(200).send('Hello World')),
      app.get('/users/{userId}', (req, res, params) => res.status(200).send(`Hello user ${params.userId}`)),
      app.get('/users/{userId}/document/{docName}', (req, res, params) => res.status(200).send(`Hello user ${params.userId}. I like your document ${params.docName}`)),
    ])
    

    In your project's root, simply add a appconfig.json that looks like this:

    {
      "headers": {
        "Access-Control-Allow-Methods": "GET, HEAD, OPTIONS, POST",
        "Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept",
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Max-Age": "1296000"
      }
    }
    

    Hope this helps.

提交回复
热议问题