500 Error with preflight Options HTTP call and CORS

I’m outputting my Kirby content as a JSON API and using an Access-Control-Allow-Origin header to get the content from a different domain. The problem is that when I am trying to consume the API I get a 500 error when it sends the preflight OPTIONS request and this as the output:

{"status":"error","message":"Invalid route or request method"}

It seems that Kirby does not like the OPTIONS request but as I understand it, this is necessary for a CORS request. Any ideas on how to make this work?

After playing with it I fixed the issue by adding this to my config:

c::set('routes', array(
  array(
    'pattern' => '(:any)',
    'action'  => function() {
      header('Access-Control-Allow-Origin: http://localhost:3000');
      header('Access-Control-Allow-Headers: X-Requested-With');   
    },
    'method' => 'OPTIONS'
  )
));

Obviously replacing the url http://localhost:3000 with wherever your frontend is running.