Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the CORS middleware.
Express
const cors = require('[1]');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'express' instead of 'cors' for CORS middleware.
Forgetting to install the 'cors' package before requiring it.
✗ Incorrect
The cors package is used to configure allowed origins in Express apps.
2fill in blank
mediumComplete the code to allow all origins using CORS middleware.
Express
app.use(cors({ origin: [1] })); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using boolean true instead of '*' to allow all origins.
Setting origin to false disables CORS.
✗ Incorrect
Setting origin: '*' allows requests from any origin.
3fill in blank
hardFix the error in the code to allow only 'http://example.com' origin.
Express
app.use(cors({ origin: [1] })); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using an array instead of a string for a single origin.
Not quoting the URL string.
✗ Incorrect
The origin value must be a string for a single allowed origin.
4fill in blank
hardFill both blanks to allow only 'http://site1.com' and 'http://site2.com' origins.
Express
const allowedOrigins = [[1], [2]]; app.use(cors({ origin: function(origin, callback) { if (!origin || allowedOrigins.indexOf(origin) !== -1) { callback(null, true); } else { callback(new Error('Not allowed by CORS')); } }}));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted URLs in the array.
Including origins not specified in the allowed list.
✗ Incorrect
We list allowed origins as strings in an array, then check if the request origin is in that list.
5fill in blank
hardFill all three blanks to configure CORS with multiple allowed origins and enable credentials.
Express
const allowed = [[1], [2]]; app.use(cors({ origin: function(origin, callback) { if (!origin || allowed.indexOf(origin) !== -1) { callback(null, true); } else { callback(new Error('CORS not allowed')); } }, [3]: true }));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'methods' instead of 'credentials' to enable credentials.
Not quoting the URLs in the array.
✗ Incorrect
We list allowed origins as strings, then set credentials: true to allow cookies and auth headers.