Giter Site home page Giter Site logo

Comments (3)

HugoPoi avatar HugoPoi commented on April 23, 2024 4

Same for problem for me. Using app.use(cors()); do nothing.
After digging during 2 hours, I figure out why I can't have CORS header on GET/POST/PUT....
First, CORS OPTIONS pre-flight works as expected with app.options(*, cors());
Second, when i use app.get('/test', cors(), function(req, res){res.json("Message with cors")}) works as expected.
My setup use Express 3.x and use app.use(app.router); before app.use(cors());. This register all middlewares behind this line after the request being handled.
Just place app.use(cors()); before app.use(app.router); or remove app.use(app.router);

from cors.

rkyleg avatar rkyleg commented on April 23, 2024

Thanks @HugoPoi, that was my problem as well.

from cors.

stouf avatar stouf commented on April 23, 2024

I took a look at the code and this behavior is apparently intended:

if (method === 'OPTIONS') {
      // preflight
      headers.push(configureOrigin(options, req));
      headers.push(configureCredentials(options, req));
      headers.push(configureMethods(options, req));
      headers.push(configureAllowedHeaders(options, req));
      headers.push(configureMaxAge(options, req));
      headers.push(configureExposedHeaders(options, req));
      applyHeaders(headers, res);

      if (options.preflightContinue ) {
        next();
      } else {
        res.statusCode = options.optionsSuccessStatus || defaults.optionsSuccessStatus;
        res.end();
      }
    } else {
      // actual response
      headers.push(configureOrigin(options, req));
      headers.push(configureCredentials(options, req));
      headers.push(configureExposedHeaders(options, req));
      applyHeaders(headers, res);
      next();
    }
  }

It also seems to be part of the standard definition. According to MDN, some headers like Access-Control-Max-Age and Access-Control-Allow-Methods for example will only be used for preflight requests.

The Access-Control-Max-Age header indicates how long the results of a preflight request can be cached. For an example of a preflight request, see the above examples.

The Access-Control-Allow-Methods header specifies the method or methods allowed when accessing the resource. This is used in response to a preflight request. The conditions under which a request is preflighted are discussed above.

Some others however (like Access-Control-Allow-Origin and Access-Control-Allow-Credentials for example) will be used for every request, regardless of the HTTP method.

from cors.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.