0
0
Expressframework~20 mins

Preflight requests behavior in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Preflight Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when an Express server receives a CORS preflight OPTIONS request?

Consider an Express server configured with CORS middleware. When the browser sends a preflight OPTIONS request, what is the typical server response behavior?

Express
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);
AThe server ignores OPTIONS requests and returns 404 Not Found.
BThe server responds with status 200 and the JSON data from the GET /data route.
CThe server responds with status 204 and appropriate CORS headers without calling other route handlers.
DThe server responds with status 500 Internal Server Error due to missing OPTIONS handler.
Attempts:
2 left
💡 Hint

Think about how CORS middleware handles OPTIONS requests before other routes.

📝 Syntax
intermediate
2:00remaining
Which Express code snippet correctly handles CORS preflight requests for all routes?

Choose the code snippet that properly enables CORS preflight OPTIONS requests for every route in an Express app.

Aapp.use(cors()); app.options('*', cors());
Bapp.get('*', cors()); app.options('*', cors());
Capp.use(cors()); app.get('*', cors());
Dapp.options('*', (req, res) => res.sendStatus(200));
Attempts:
2 left
💡 Hint

Remember that preflight requests use the OPTIONS method and need special handling.

🔧 Debug
advanced
2:00remaining
Why does this Express server fail to respond correctly to CORS preflight requests?

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?

Express
const express = require('express');
const app = express();
app.get('/api', (req, res) => res.json({data: 'ok'}));
app.listen(3000);
AThe GET route handler incorrectly responds to OPTIONS requests causing errors.
BThe server uses express but does not import the http module.
CThe server listens on port 3000 which is blocked by the browser.
DThe server lacks CORS middleware and does not handle OPTIONS requests, so preflight fails.
Attempts:
2 left
💡 Hint

Think about what the browser expects before sending actual requests with custom headers.

state_output
advanced
2:00remaining
What is the response status code for a successful CORS preflight request in Express with cors middleware?

Given an Express app using the cors middleware, what status code does the server send back for a successful OPTIONS preflight request?

Express
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors());
app.options('*', cors());
app.listen(3000);
A204 No Content
B200 OK
C404 Not Found
D500 Internal Server Error
Attempts:
2 left
💡 Hint

Preflight requests do not return data, only headers.

🧠 Conceptual
expert
2:00remaining
Which header must the Express server include in the preflight response to allow credentials in CORS requests?

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?

AAccess-Control-Allow-Origin: *
BAccess-Control-Allow-Credentials: true
CAccess-Control-Allow-Methods: GET, POST, OPTIONS
DAccess-Control-Allow-Headers: Content-Type
Attempts:
2 left
💡 Hint

Credentials require a specific header that cannot be wildcarded.