Consider an Express server configured with CORS middleware. When the browser sends a preflight OPTIONS request, what is the typical server response behavior?
const express = require('express'); const cors = require('cors'); const app = express(); app.use(cors()); app.options('*', cors()); app.get('/data', (req, res) => res.json({msg: 'Hello'})); app.listen(3000);
Think about how CORS middleware handles OPTIONS requests before other routes.
Express CORS middleware automatically handles preflight OPTIONS requests by sending a 204 No Content response with the correct CORS headers. It does not proceed to other route handlers.
Choose the code snippet that properly enables CORS preflight OPTIONS requests for every route in an Express app.
Remember that preflight requests use the OPTIONS method and need special handling.
Using app.use(cors()) enables CORS for all routes and methods. Adding app.options('*', cors()) ensures OPTIONS preflight requests are handled with proper CORS headers.
Examine the code below. The server does not respond properly to preflight OPTIONS requests, causing CORS errors in the browser. What is the main issue?
const express = require('express'); const app = express(); app.get('/api', (req, res) => res.json({data: 'ok'})); app.listen(3000);
Think about what the browser expects before sending actual requests with custom headers.
Without CORS middleware, Express does not send the required CORS headers or handle OPTIONS preflight requests, so browsers block the request.
Given an Express app using the cors middleware, what status code does the server send back for a successful OPTIONS preflight request?
const express = require('express'); const cors = require('cors'); const app = express(); app.use(cors()); app.options('*', cors()); app.listen(3000);
Preflight requests do not return data, only headers.
The CORS middleware responds to OPTIONS preflight requests with status 204 No Content, indicating success without a response body.
When handling CORS preflight OPTIONS requests, which response header must the Express server include to allow the browser to send credentials (cookies, HTTP auth) with cross-origin requests?
Credentials require a specific header that cannot be wildcarded.
The Access-Control-Allow-Credentials: true header tells the browser it can include credentials in cross-origin requests. The origin cannot be '*'.