# CORS on multiple methods in same function

**URL:** <https://forum.serverless.com/t/cors-on-multiple-methods-in-same-function/1851>\
**Category:** Serverless Framework\
**Created:** [May 12, 2017, 6:16am UTC](https://forum.serverless.com/t/cors-on-multiple-methods-in-same-function/1851 "2017-05-12T06:16:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rcdelacruz](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/rcdelacruz/32/650_2.png) [@rcdelacruz](https://forum.serverless.com/u/rcdelacruz)\
**Post date:** [May 12, 2017, 6:16am UTC](https://forum.serverless.com/t/cors-on-multiple-methods-in-same-function/1851/1 "2017-05-12T06:16:51Z")

</div>

Hi,

I think this is related to this issue: [https://github.com/serverless/serverless/issues/1960](https://github.com/serverless/serverless/issues/1960)  
I am trying to organize my CRUD functionalities into one function. However, I am having problems with CORS. I always get this error:

Fetch API cannot load [https://xxxx.execute-api.ap-southeast-1.amazonaws.com/prod/v1/xxx](https://xxxx.execute-api.ap-southeast-1.amazonaws.com/prod/v1/xxx). No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘[http://localhost:3000](http://localhost:3000)’ is therefore not allowed access

Can someone help me pls? Thanks

---

<div class="post-metadata">

**Author:** ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)\
**Post date:** [May 12, 2017, 7:25am UTC](https://forum.serverless.com/t/cors-on-multiple-methods-in-same-function/1851/2 "2017-05-12T07:25:18Z")

</div>

It’s much more likely you’re simply not setting the `Access-Control-Allow-Origin` header. Are you setting the header in the response?

```javascript
{
  statusCode: 200,
  headers: {
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Credentials": true
  },
  body: JSON.stringify({ ...return object goes here... })
}

```

Don’t forget to also set `cors: true` inside your `serverless.yml`.

---

<div class="post-metadata">

**Author:** ![rcdelacruz](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/rcdelacruz/32/650_2.png) [@rcdelacruz](https://forum.serverless.com/u/rcdelacruz)\
**Post date:** [May 12, 2017, 9:22am UTC](https://forum.serverless.com/t/cors-on-multiple-methods-in-same-function/1851/3 "2017-05-12T09:22:31Z")

</div>

Yup, I did that. It works on single function and single method approach, but not on multiple methods in same function. I want something like this:

```auto
users:
    handler: handler.main
    events:
      - http:
          path: v1/users
          method: post
          cors: true
          
      - http:
          path: v1/users/{id}
          method: get
          cors: true
         
      - http:
          path: v1/users
          method: get
          cors: true

      - http:
          path: v1/users/{id}
          method: put
          cors: true

      - http:
          path: v1/users/{id}
          method: delete
          cors: true

```

---

<div class="post-metadata">

**Author:** ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)\
**Post date:** [May 12, 2017, 12:24pm UTC](https://forum.serverless.com/t/cors-on-multiple-methods-in-same-function/1851/4 "2017-05-12T12:24:51Z")

</div>

If you’re routing everything through a single Lambda then you can simplify the config by dealing with invalid method/path combinations in the Lambda. This reduces your config to:

```yaml
users:
    handler: handler.main
    events:
      - http:
          path: v1/users
          method: any
          cors: true
      - http:
          path: v1/users/{id}
          method: any
          cors: true

```

I wrote a post back in December about doing exactly what you’re trying to do. It didn’t have CORS support then but I’ve just added that. You get view it [here](https://www.richdevelops.dev/routing-api-gateway-traffic-through-one-lamda-function) including the source/settings (all tested).

If you’re still stuck then can you post your Lambda source?

---

<div class="post-metadata">

**Author:** ![rcdelacruz](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/rcdelacruz/32/650_2.png) [@rcdelacruz](https://forum.serverless.com/u/rcdelacruz)\
**Post date:** [May 15, 2017, 4:50am UTC](https://forum.serverless.com/t/cors-on-multiple-methods-in-same-function/1851/5 "2017-05-15T04:50:11Z")

</div>

Your github link is not available.

Though, I tried your solution, but still doesn’t work.

---

<div class="post-metadata">

**Author:** ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)\
**Post date:** [May 15, 2017, 6:24am UTC](https://forum.serverless.com/t/cors-on-multiple-methods-in-same-function/1851/6 "2017-05-15T06:24:33Z")

</div>

What browser are you using?

Can you paste the error message here?

Can you make your code public (both the handler and the `serverless.yml`)?

---

<div class="post-metadata">

**Author:** ![rcdelacruz](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/rcdelacruz/32/650_2.png) [@rcdelacruz](https://forum.serverless.com/u/rcdelacruz)\
**Post date:** [May 15, 2017, 7:30am UTC](https://forum.serverless.com/t/cors-on-multiple-methods-in-same-function/1851/7 "2017-05-15T07:30:08Z")

</div>

Using Chrome -

 ![](https://canada1.discourse-cdn.com/flex036/uploads/serverless/original/1X/c91aad2d267c5d1194e57e70eca0c3493bd688e9.png)

> <https://gist.github.com/rcdelacruz/e31f8b6dd32fadcf6a72d2684f25f069>

---

<div class="post-metadata">

**Author:** ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)\
**Post date:** [May 15, 2017, 10:23am UTC](https://forum.serverless.com/t/cors-on-multiple-methods-in-same-function/1851/8 "2017-05-15T10:23:33Z")

</div>

The real problem is the 502 error from your API. Something is going wrong in your handler and the API Gateway is generating the response rather than your Lambda so the headers aren’t being set. Sort that out and it should work.

Here are a few things I can see:

Your `main` function shouldn’t be `async`.

```javascript
export async function main(event, context, callback) {

```

Your `main` function doesn’t return a result (by calling `callback`) under all circumstances. If you’re using lambda proxy integration then it’s your responsibility to return an error.

In `main` you’re calling:

```javascript
return handlers[httpMethod](event, context, callback);

```

which calls

```javascript
async function listItems(event, context, callback) {

```

but `listItems` is `async` so you should `await` the result. Having said that, I actually think you don’t want it to be `async` either as you’re returning via the `callback`.

Hopefully this help.

---

<div class="post-metadata">

**Author:** ![rcdelacruz](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/rcdelacruz/32/650_2.png) [@rcdelacruz](https://forum.serverless.com/u/rcdelacruz)\
**Post date:** [May 15, 2017, 11:15am UTC](https://forum.serverless.com/t/cors-on-multiple-methods-in-same-function/1851/9 "2017-05-15T11:15:19Z")

</div>

Thanks for checking it out. But this code works if there is a single method in a function.
