What does CORS stand for and why is it important in web development?
CORS stands for Cross-Origin Resource Sharing. It is important because it controls how resources on a web server can be requested from another domain, helping to keep web applications secure by preventing unauthorized access.
Click to reveal answer
beginner
How do you enable CORS in a Node.js Express app using the 'cors' package?
You install the 'cors' package and then use it as middleware in your Express app like this: <br><code>const cors = require('cors');<br>app.use(cors());</code><br>This allows all cross-origin requests by default.
Click to reveal answer
intermediate
What is the purpose of setting specific origins in CORS configuration?
Setting specific origins restricts which domains can access your server resources. This improves security by only allowing trusted websites to make requests.
Click to reveal answer
intermediate
Explain the difference between simple requests and preflight requests in CORS.
Simple requests are basic HTTP requests that browsers allow without extra checks. Preflight requests are OPTIONS requests sent by the browser to check if the actual request is safe to send, used for methods like PUT or DELETE or when custom headers are involved.
Click to reveal answer
intermediate
How can you configure CORS in Express to allow only GET and POST methods from 'https://example.com'?
You can configure CORS like this: app.use(cors({ origin: 'https://example.com', methods: ['GET', 'POST'] })); This restricts access to only GET and POST requests from that origin.
Click to reveal answer
What does the 'Access-Control-Allow-Origin' header do?
ASpecifies which origins can access the resource
BBlocks all cross-origin requests
CEncrypts data sent between client and server
DSpecifies the HTTP methods allowed
✗ Incorrect
The 'Access-Control-Allow-Origin' header tells the browser which origins are allowed to access the resource.
Which HTTP method is used for CORS preflight requests?
AOPTIONS
BPOST
CGET
DPUT
✗ Incorrect
Browsers send an OPTIONS request as a preflight to check if the actual request is safe.
In Express, which package is commonly used to handle CORS?
Aexpress-cors
Bcors-handler
Ccross-origin
Dcors
✗ Incorrect
The 'cors' package is the standard middleware to enable CORS in Express apps.
What happens if you do not configure CORS on your server?
AAll cross-origin requests are allowed by default
BYour server sends an error response
CBrowsers block cross-origin requests by default
DYour server crashes
✗ Incorrect
Browsers block cross-origin requests unless the server explicitly allows them via CORS headers.
Which of these is NOT a valid CORS configuration option in the 'cors' package?
Amethods
Bencryption
Ccredentials
Dorigin
✗ Incorrect
'encryption' is not a CORS configuration option; CORS deals with access control, not encryption.
Describe how to set up CORS in a Node.js Express app to allow only requests from 'https://myapp.com' and only GET and POST methods.
Think about the options object passed to the cors middleware.
You got /4 concepts.
Explain why browsers use preflight requests and how they relate to CORS configuration.
Consider what happens before the main request is sent.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of configuring CORS in a Node.js server?
easy
A. To speed up server response time
B. To control which websites can access server resources
C. To encrypt data sent between client and server
D. To manage database connections
Solution
Step 1: Understand CORS role
CORS (Cross-Origin Resource Sharing) controls which external websites can request resources from your server.
Step 2: Identify main purpose
It is not about speed, encryption, or database management but about access control from different origins.
Final Answer:
To control which websites can access server resources -> Option B
Quick Check:
CORS controls access = A [OK]
Hint: Remember CORS = Cross-Origin Resource Sharing control [OK]
Common Mistakes:
Confusing CORS with encryption or speed optimization
Thinking CORS manages databases
Assuming CORS is a security firewall
2. Which of the following is the correct way to import and use the cors middleware in an Express app?
easy
A. import cors from 'cors'; app.use(cors());
B. import cors from 'cors'; app.use(cors);
C. const cors = require('cors'); app.use(cors);
D. const cors = require('cors'); app.use(cors());
Solution
Step 1: Identify modern import syntax
Using ES modules, the correct import is import cors from 'cors';.
Step 2: Use middleware correctly
Middleware must be called as a function: app.use(cors()); not just cors.
Final Answer:
import cors from 'cors'; app.use(cors()); -> Option A
Quick Check:
Import + call middleware = D [OK]
Hint: Use import + call cors() as function [OK]
Common Mistakes:
Forgetting parentheses when using middleware
Mixing require and import syntax
Using app.use(cors) without parentheses
3. Given this code snippet, what will be the CORS behavior?
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);
medium
A. Only requests from https://example.com will be allowed
B. Requests from any origin will be allowed
C. No requests will be allowed due to missing headers
D. Requests from localhost will be allowed only
Solution
Step 1: Analyze the origin option
The cors middleware is configured with origin: 'https://example.com', which restricts access to that origin only.
Step 2: Understand effect on requests
Requests from other origins will be blocked by CORS policy, so only https://example.com is allowed.
Final Answer:
Only requests from https://example.com will be allowed -> Option A
Quick Check:
origin restricts access = C [OK]
Hint: Check origin value to know allowed sites [OK]
Common Mistakes:
Assuming all origins allowed by default
Confusing origin with methods or headers
Thinking localhost is allowed without explicit mention
4. Regarding this CORS setup code, which statement is accurate?
import express from 'express';
import cors from 'cors';
const app = express();
app.use(cors({ origin: ['https://site1.com', 'https://site2.com'] }));
app.listen(3000);
medium
A. Missing call to cors() function
B. The app.listen() call is missing a callback
C. The origin option can be an array
D. The cors package is not imported correctly
Solution
Step 1: Check origin option type
The origin option in cors accepts an array directly for multiple static origins; it also supports string, boolean, regex, or function.
Step 2: Confirm other parts are correct
The cors() function is called correctly, app.listen() works without callback, and import syntax is valid.
Final Answer:
The origin option can be an array -> Option C
Quick Check:
origin array valid = B [OK]
Hint: origin can be string, array, regex, or function [OK]
Common Mistakes:
Thinking origin cannot accept an array
Forgetting to call cors() as a function
Thinking app.listen needs a callback always
5. You want to allow CORS requests only from https://trusted.com and http://localhost:3000. Which is the best way to configure the cors middleware in your Node.js app?
hard
A. Use origin as a function to check allowed origins dynamically
B. Set origin to '*' to allow all origins
C. Disable CORS middleware and handle manually
D. Set origin to an array of allowed origins directly
Solution
Step 1: Understand multiple origins handling
The cors package accepts an array directly for origin with a static list of allowed origins. This is the simplest and best approach here.
Step 2: Evaluate options
Use function for dynamic logic (e.g., regex or env-based). '*' allows all (insecure). Manual handling is error-prone and not recommended.
Final Answer:
Set origin to an array of allowed origins directly -> Option D
Quick Check:
Static multiple origins use array = C [OK]
Hint: Use array for static multiple origins [OK]
Common Mistakes:
Using '*' to allow all origins insecurely
Using a single string for multiple origins
Disabling middleware instead of configuring properly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.