Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Configuring Allowed Origins in Express
📖 Scenario: You are building a simple Express server that will accept requests only from certain websites. This helps keep your server safe by allowing only trusted websites to talk to it.
🎯 Goal: Build an Express server that uses a list of allowed origins to control which websites can access it.
📋 What You'll Learn
Create an Express app
Define a list of allowed origins
Write middleware to check the request origin against the allowed list
Set the CORS headers only if the origin is allowed
💡 Why This Matters
🌍 Real World
Many web servers need to control which websites can access their resources to prevent unauthorized use and security risks.
💼 Career
Understanding how to configure allowed origins and CORS headers is essential for backend developers working with APIs and web servers.
Progress0 / 4 steps
1
Create the Express app
Write code to import express and create an Express app called app.
Express
Hint
Use require('express') to import Express and then call express() to create the app.
2
Define allowed origins list
Create a constant array called allowedOrigins with these exact strings: 'http://example.com' and 'http://localhost:3000'.
Express
Hint
Use an array with the exact URLs as strings.
3
Add middleware to check origin
Add middleware to app that checks if req.headers.origin is in allowedOrigins. If yes, set the header Access-Control-Allow-Origin to that origin. Use app.use and a function with parameters req, res, and next.
Express
Hint
Use app.use to add middleware. Check if origin is in allowedOrigins and set the header accordingly.
4
Start the server
Add code to make app listen on port 4000 using app.listen.
Express
Hint
Use app.listen(4000) to start the server on port 4000.
Practice
(1/5)
1. What is the main purpose of configuring allowed origins in an Express app using cors middleware?
easy
A. To encrypt data sent between client and server
B. To speed up the server response time
C. To control which websites can access your server resources
D. To log all incoming requests
Solution
Step 1: Understand what allowed origins mean
Allowed origins specify which websites are permitted to make requests to your server.
Step 2: Identify the role of cors middleware
The cors middleware in Express helps set these allowed origins to control access.
Final Answer:
To control which websites can access your server resources -> Option C
Quick Check:
Allowed origins = control access [OK]
Hint: Allowed origins control access, not speed or encryption [OK]
Common Mistakes:
Confusing allowed origins with encryption
Thinking it speeds up server
Assuming it logs requests
2. Which of the following is the correct way to allow only 'https://example.com' as an origin using the cors middleware in Express?
easy
A. app.use(cors({ origin: [https://example.com] }));
B. app.use(cors({ origin: 'https://example.com' }));
C. app.use(cors({ origins: 'https://example.com' }));
D. app.use(cors(https://example.com));
Solution
Step 1: Check the correct option name for allowed origins
The correct option is origin, not origins.
Step 2: Verify the value type for origin
It accepts a string for a single allowed origin, so 'https://example.com' is correct.
Final Answer:
app.use(cors({ origin: 'https://example.com' })); -> Option B
Quick Check:
Option name is origin, value is string [OK]
Hint: Use 'origin' option with string for single allowed site [OK]
Common Mistakes:
Using 'origins' instead of 'origin'
Passing array for single origin string
Calling cors without options
3. Given this Express code snippet, what will be the result when a request comes from 'https://allowed.com'?
const cors = require('cors');
app.use(cors({ origin: ['https://allowed.com', 'https://other.com'] }));
medium
A. The request will be allowed because 'https://allowed.com' is in the list
B. The request will be blocked due to invalid origin format
C. The request will be allowed only if it uses POST method
D. The request will be blocked because origin must be a string
Solution
Step 1: Understand the origin option accepts an array
The origin option can accept an array of allowed origins to permit multiple sites.
Step 2: Check if 'https://allowed.com' is in the array
Since 'https://allowed.com' is listed, requests from it will be allowed.
Final Answer:
The request will be allowed because 'https://allowed.com' is in the list -> Option A
Quick Check:
Array of origins allows listed sites [OK]
Hint: Array of origins lets listed sites access [OK]
Common Mistakes:
Thinking origin must be string only
Assuming method affects origin check
Believing array format causes error
4. Identify the error in this Express CORS setup that aims to allow only 'https://site.com':
A. Calling cors() twice causes conflict and overrides settings
B. The methods option is invalid in cors middleware
C. The origin value should be an array, not a string
D. Missing next() call in middleware
Solution
Step 1: Check middleware usage order
Calling cors() twice means the second call overrides the first, ignoring origin restrictions.
Step 2: Confirm methods option is valid
The methods option is valid to restrict HTTP methods, so no error there.
Final Answer:
Calling cors() twice causes conflict and overrides settings -> Option A
Quick Check:
Multiple cors calls override previous config [OK]
Hint: Only call cors once with all options [OK]
Common Mistakes:
Calling cors middleware multiple times
Thinking origin must be array always
Ignoring middleware order effects
5. You want to allow requests only from origins that end with '.trusted.com' dynamically in Express. Which cors configuration correctly implements this?
hard
A. app.use(cors({ origin: ['*.trusted.com'] }));
B. app.use(cors({ origin: (origin, callback) => { if (origin.includes('.trusted.com')) callback(null, true); else callback(new Error('Not allowed')); } }));
C. app.use(cors({ origin: '/^https:\/\/.*\.trusted\.com$/' }));
D. app.use(cors({ origin: (origin, callback) => { if (origin.endsWith('.trusted.com')) callback(null, true); else callback(new Error('Not allowed')); } }));
Solution
Step 1: Understand dynamic origin checking
To allow origins ending with '.trusted.com', a function can check the origin string dynamically.
Step 2: Evaluate each option's approach
app.use(cors({ origin: (origin, callback) => { if (origin.endsWith('.trusted.com')) callback(null, true); else callback(new Error('Not allowed')); } })); uses a function with endsWith to precisely match the domain ending, which is correct. app.use(cors({ origin: ['*.trusted.com'] })); uses wildcard string which is not supported. app.use(cors({ origin: '/^https:\/\/.*\.trusted\.com$/' })); uses regex but cors does not accept regex directly. app.use(cors({ origin: (origin, callback) => { if (origin.includes('.trusted.com')) callback(null, true); else callback(new Error('Not allowed')); } })); uses includes which may allow unwanted matches.
Final Answer:
app.use(cors({ origin: (origin, callback) => { if (origin.endsWith('.trusted.com')) callback(null, true); else callback(new Error('Not allowed')); } })); -> Option D
Quick Check:
Use function with endsWith for dynamic origin [OK]
Hint: Use function with endsWith() to allow domain patterns [OK]