0
0
Expressframework~10 mins

cors middleware setup in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - cors middleware setup
Start Express App
Import cors Middleware
Configure cors Options
Use cors Middleware in App
Handle Incoming Requests
cors Checks Request Origin
Allow or Block Request
Send Response
This flow shows how the Express app imports and uses the cors middleware to check and allow cross-origin requests before sending responses.
Execution Sample
Express
import express from 'express';
import cors from 'cors';

const app = express();
app.use(cors({ origin: 'https://example.com' }));

app.get('/', (req, res) => res.send('Hello'));
app.listen(3000);
This code sets up an Express server that uses cors middleware to allow requests only from https://example.com.
Execution Table
StepActioncors Middleware BehaviorRequest OriginAllowed?Response Sent
1Server starts and listens on port 3000cors middleware readyN/AN/AN/A
2Incoming GET request to '/' from https://example.comChecks origin headerhttps://example.comYesHello
3Incoming GET request to '/' from https://notallowed.comChecks origin headerhttps://notallowed.comNoBlocked by CORS
4Incoming GET request to '/' with no origin header (same origin)No origin, allowed by defaultNoneYesHello
5Server continues listening for requestscors middleware activeN/AN/AN/A
💡 Requests blocked if origin is not https://example.com; otherwise allowed and responded.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
appExpress instanceExpress instanceExpress instanceExpress instanceExpress instance
corsOptions.origin'https://example.com''https://example.com''https://example.com''https://example.com''https://example.com'
request.originN/A'https://example.com''https://notallowed.com'NoneN/A
request.allowedN/AtruefalsetrueN/A
Key Moments - 3 Insights
Why does the request from https://notallowed.com get blocked?
Because the cors middleware checks the origin against the allowed origin 'https://example.com' and blocks any origin not matching, as shown in execution_table row 3.
What happens if the request has no origin header?
Requests without an origin header are treated as same-origin and allowed by default, as shown in execution_table row 4.
Where in the code is the allowed origin set?
The allowed origin is set in the cors middleware options passed to app.use(cors({ origin: 'https://example.com' })), as tracked in variable_tracker under corsOptions.origin.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the 'Allowed?' value at Step 3?
AN/A
BYes
CNo
DDepends on method
💡 Hint
Check the 'Allowed?' column in execution_table row 3 for the request from https://notallowed.com.
At which step does the cors middleware allow a request with no origin header?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at execution_table row 4 where the origin is 'None' and 'Allowed?' is 'Yes'.
If we change the allowed origin to '*' in cors options, what changes in the execution table?
ANo origins are allowed, so all requests blocked
BAll origins are allowed, so Step 3 'Allowed?' becomes Yes
COnly requests from localhost are allowed
DNo change, only https://example.com allowed
💡 Hint
Changing origin to '*' means all origins pass the check, affecting the 'Allowed?' column in execution_table.
Concept Snapshot
Express CORS Middleware Setup:
1. Import cors and express.
2. Create app with express().
3. Use app.use(cors({ origin: 'allowed-origin' })) to set allowed origins.
4. cors middleware checks incoming request origins.
5. Requests from allowed origins proceed; others blocked.
6. Helps secure cross-origin resource sharing easily.
Full Transcript
This visual execution trace shows how to set up CORS middleware in an Express app. The app imports the cors package and uses it with options specifying allowed origins. When requests come in, the middleware checks the origin header. If the origin matches the allowed origin, the request is allowed and the response is sent. If not, the request is blocked by CORS. Requests without an origin header are treated as same-origin and allowed. The execution table tracks each step, showing how requests from different origins are handled. The variable tracker follows key variables like the app instance, cors options, request origin, and whether the request is allowed. Key moments clarify common confusions about origin checking and default behaviors. The quiz tests understanding of how the middleware behaves at different steps and how changing options affects request allowance. This setup helps developers control which external sites can access their server resources safely.