Ninja Framework preflight

100 Views Asked by At

Is there a particular way to preflight in Ninja Framework?

I am trying to allow all preflight requests, however due to the fact that I don't have routes for everything with the options method, it is coming up as 404 and failing.

Essentially, I am getting this error and trying to get rid of it:

Access to XMLHttpRequest at 'https://example.com/path/to/endpoint' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Is there a way to capture ALL options methods and allow them? Rather than re-writing each and every route with an options request?

i.e. instead of:

router.GET().route("/index.html").with(ApplicationController::index);
router.OPTIONS().route("/index.html").with(ApplicationController::index);
router.GET().route("/user").with(ApplicationController::user);
router.OPTIONS().route("/user").with(ApplicationController::user);

It would instead be something like this (pseudo-code below):

router.OPTIONS().route(<All Routes>).respond("200 OK");
1

There are 1 best solutions below

0
Steven Matthews On

The solution is this:

    router.OPTIONS().route("/.*").with(Controller::method);

It now catches all preflight requests