question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple cors middlewares on same request?

See original GitHub issue

I have an API where I want every path to be CORS-enabled for all origins except for /auth/connect/twitter because it uses a cookie based session.

I was wondering if it would be possible to make it so that the last cors() middleware always overrides precedent ones. E.g.

router.use(cors())
router.use(cors({ origin: 'http://mydomain.com' }))

should set the Access-Control-Allow-Origin to http://mydomain.com. Is that possible?

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:6
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
olalondecommented, Jun 4, 2016

Yes, I ended up doing something like this:

    const corsMiddleware = cors()
    router.use((req, res, next) => {
      if (req.path.match(/^\/auth\/connect\//)) {
        // let later cors middleware handle it!
        return next()
      }
      return corsMiddleware(req, res, next)
    })
0reactions
aman-kacommented, Apr 25, 2021

hey Hii every one i am new to opensource world can any one help me in making my first contribution in this library.by explaining it more to me and guiding me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Allow multiple CORS domain in express js - Stack Overflow
Using Node, cors middleware, make sure you leave the / off the url, ... This will allow request from localhost:3000 and origin2.com.
Read more >
Rails CORS Middleware For Multiple Resources · Daniela Baron
In my case, I have a Rails server that provides some back end services for this blog (which is statically hosted on Github...
Read more >
CORS for multiple Domains in Node and Express - Saumya
It allows to use resources from one server in another server. Well simply said it enables to call, APIs on one server, from...
Read more >
Express cors middleware
CORS is a node.js package for providing a Connect/Express middleware that can be ... An example of a 'complex' CORS request is one...
Read more >
Using CORS in Express - Medium
Cross-origin resource sharing (CORS) allows AJAX requests to skip the Same-origin policy and access resources from remote hosts. In this post I will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found