Short question. I'm building this app and it's getting quite large and messy so I'd like to reduce it. I have app.use(cors()) in server.js and define router.use(cors()) for every route that I have. Can I remove router.use(cors()) on every route and not have a cors error by only having app.use(cors()) ?
CodePudding user response:
You will not get any error if you have app.use(cors()) only in server.js file.
CodePudding user response:
One copy of:
app.use(cors());
That occurs BEFORE any of your route or router definitions are registered will automatically apply to all your route definitions whether they are app.xxx()
or router.xxx()
.
Middleware is run in the order you register it so as long as app.use(cors())
runs before you register any of your routers, it will automatically apply to all of your routers.