Complete the code to import the CORS middleware.
const cors = require('[1]');
The cors package is used to configure allowed origins in Express apps.
Complete the code to allow all origins using CORS middleware.
app.use(cors({ origin: [1] }));Setting origin: '*' allows requests from any origin.
Fix the error in the code to allow only 'http://example.com' origin.
app.use(cors({ origin: [1] }));The origin value must be a string for a single allowed origin.
Fill both blanks to allow only 'http://site1.com' and 'http://site2.com' origins.
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')); } }}));
We list allowed origins as strings in an array, then check if the request origin is in that list.
Fill all three blanks to configure CORS with multiple allowed origins and enable credentials.
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 }));
We list allowed origins as strings, then set credentials: true to allow cookies and auth headers.
