node8-express-template icon indicating copy to clipboard operation
node8-express-template copied to clipboard

Suggestion - support OPTIONS verb

Open nerdguru opened this issue 5 years ago • 4 comments

Hi there,

When using OpenFaaS and Node.js to build an API accessible by Javascript running in the browser, browsers will make a preflight OPTIONS call prior to a POST. As currently constructed, a function can respond to POST and GET verbs where they can choose to alter the Access-Control-Allow-Origin header so that CORS calls can be made in the browser, but not OPTIONS.

As a workaround for my own code, I simply added the following in index.js on line 85:

app.options("/*", function(req, res, next){ res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS'); res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With'); res.send(200); });

However, I'm not sure that's the best long term fix. I thought I'd document this here and I'm willing to submit a PR for what I've done if you're interested.

nerdguru avatar Sep 06 '18 23:09 nerdguru