Preflight requests check if a web server allows certain actions from a browser before sending the real request. This helps keep your app safe.
Preflight requests behavior in Express
app.options(path, middleware, handler)
app.options handles HTTP OPTIONS requests, which are used for preflight checks.
You can add middleware to set headers like Access-Control-Allow-Origin here.
app.options('/api/data', (req, res) => { res.set('Access-Control-Allow-Origin', '*'); res.set('Access-Control-Allow-Methods', 'GET,POST,PUT'); res.set('Access-Control-Allow-Headers', 'Content-Type'); res.sendStatus(204); });
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
});This Express app sets CORS headers for all requests. It specifically handles OPTIONS requests to '/data' by allowing GET, POST, and PUT methods. When a browser sends a preflight request, the server replies with allowed methods and headers. The PUT route responds with a simple message.
import express from 'express'; const app = express(); // Middleware to handle CORS headers app.use((req, res, next) => { res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Headers', 'Content-Type'); next(); }); // Handle preflight OPTIONS request app.options('/data', (req, res) => { res.header('Access-Control-Allow-Methods', 'GET,POST,PUT'); res.sendStatus(204); // No Content }); // Actual route app.put('/data', (req, res) => { res.send('Data updated'); }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
Preflight requests use the HTTP OPTIONS method before the actual request.
Always respond to OPTIONS requests quickly with status 204 (No Content) to avoid delays.
Setting Access-Control-Allow-Origin to '*' allows all websites to access your API, which may not be safe for sensitive data.
Preflight requests check permissions before sending real requests.
Express handles preflight with app.options() for OPTIONS method.
Set proper CORS headers to allow or restrict cross-origin requests.
Practice
Solution
Step 1: Understand preflight request role
A preflight request uses the OPTIONS method to ask the server if the actual request is safe to send.Step 2: Identify what preflight checks
It checks allowed methods and headers to prevent unauthorized cross-origin requests.Final Answer:
To check if the server allows the actual request method and headers before sending it -> Option AQuick Check:
Preflight checks permissions = B [OK]
- Thinking preflight sends actual data
- Confusing preflight with authentication
- Assuming preflight closes connection
Solution
Step 1: Recall HTTP methods in Express
Express provides methods matching HTTP verbs like get, post, options, etc.Step 2: Identify method for preflight
Preflight requests use the OPTIONS method, so Express uses app.options() to handle them.Final Answer:
app.options() -> Option AQuick Check:
OPTIONS method handled by app.options() [OK]
- Using app.get() or app.post() for OPTIONS requests
- Confusing app.use() as handler for OPTIONS
- Not handling OPTIONS method at all
app.options('/data', (req, res) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,POST');
res.sendStatus(204);
});
What will the server respond to an OPTIONS request to '/data'?Solution
Step 1: Analyze headers set in response
The code sets 'Access-Control-Allow-Origin' to '*' allowing any origin, and 'Access-Control-Allow-Methods' to 'GET,POST'.Step 2: Check response status
res.sendStatus(204) sends HTTP status 204 (No Content) with the headers.Final Answer:
Status 204 with headers allowing any origin and GET, POST methods -> Option BQuick Check:
Preflight responds 204 + CORS headers = C [OK]
- Expecting status 200 instead of 204
- Missing CORS headers in response
- Confusing 404 or 500 errors
app.options('/api', (req, res) => {
res.header('Access-Control-Allow-Origin', 'https://example.com');
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT');
res.end();
});
But the browser still blocks the actual request. What is the likely problem?Solution
Step 1: Understand preflight header requirements
If the actual request uses custom headers, the server must respond with Access-Control-Allow-Headers listing them.Step 2: Check code for missing headers
The code does not set Access-Control-Allow-Headers, so browser blocks the request due to missing allowed headers.Final Answer:
Missing Access-Control-Allow-Headers header for custom headers -> Option CQuick Check:
Missing allowed headers header blocks request = D [OK]
- Assuming res.end() causes the block
- Thinking Access-Control-Allow-Origin must be '*' always
- Ignoring missing allowed headers header
Solution
Step 1: Match allowed origin and methods
app.options('/resource', (req, res) => { res.header('Access-Control-Allow-Origin', 'https://trusted.com'); res.header('Access-Control-Allow-Methods', 'GET, POST'); res.header('Access-Control-Allow-Headers', 'X-Custom-Auth'); res.sendStatus(204); }); sets Access-Control-Allow-Origin to 'https://trusted.com' and methods to 'GET, POST' as required.Step 2: Include custom headers and correct status
It includes Access-Control-Allow-Headers for 'X-Custom-Auth' and sends status 204 (No Content), which is correct for preflight.Final Answer:
app.options('/resource', (req, res) => { res.header('Access-Control-Allow-Origin', 'https://trusted.com'); res.header('Access-Control-Allow-Methods', 'GET, POST'); res.header('Access-Control-Allow-Headers', 'X-Custom-Auth'); res.sendStatus(204); }); -> Option DQuick Check:
Correct origin, methods, headers, and 204 status = A [OK]
- Using '*' origin when only one domain allowed
- Missing Access-Control-Allow-Headers for custom headers
- Sending wrong status code like 200 or 404
Ready to prove it?
Pass the test and this pattern moves unknown
