Update documentation regarding preflight use with other middleware
See original GitHub issueI’ve run into an issue when using a combination of middleware, particularly ones that modify the headers. As it turns out, ordering is important.
import express from 'express'
import helmet from 'helmet'
import cors from 'cors'
import morgan from 'morgan'
import bodyParser from 'body-parser'
import router from './routes'
import './utils/db'
const app = express()
app.use(cors())
app.use(morgan('combined'))
app.use(bodyParser.json({ type: 'application/json' }))
app.use(helmet())
app.use(router)
let port = 5000
app.listen(port, () => {
console.log('ready')
})
Stumbled onto this issue which reveals the issue is in this cors
middleware.
https://github.com/helmetjs/helmet/issues/157
Digging through the source code for cors
, the following line prevents subsequent middleware from being used for the preflight:
https://github.com/expressjs/cors/blob/b5bbc285194568f414afc2390c56f672734d3fac/lib/index.js#L180
I understand that this is required, but it’s unclear that the ordering of middleware is important.
I think it’s worth adding either a known issues section, or making it clear under usage
.
Issue Analytics
- State:
- Created 5 years ago
- Comments:14 (6 by maintainers)
Top Results From Across the Web
auth middleware expects credentials for CORS-preflight ...
The potential issue here is that credentials are not meant to be included in CORS-preflight OPTIONS requests, yet the middleware still expects ...
Read more >Preflight request is sent with all methods - Stack Overflow
A CORS preflight OPTIONS request can be triggered just by adding a Content-Type header to a request — if the value's anything except...
Read more >Express cors middleware
CORS is a node.js package for providing a Connect/Express middleware that can be used to enable CORS with various options. Follow me (@troygoode)...
Read more >Configuring CORS - Apollo GraphQL Docs
To do so, you'll first need to swap to using expressMiddleware (or any other Apollo Server integration). ⚠️ If your app is only...
Read more >CORS Tutorial: A Guide to Cross-Origin Resource Sharing
Tutorial on modifying existing applications to support CORS. ... will instead make an automatic preflight request using the OPTIONS method.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Understood. In that case I would certainly clarify in the README that
app.use(cors())
will enable CORS preflight requests. Currently the “Enabling CORS Pre-Flight” section leaves you under the impression thatapp.options()
is the only way.I think this confusion stems from the middleware incorrectly interpreting all
OPTIONS
requests as preflight requests. But clearly, someOPTIONS
requests are not preflight requests.