0
0
Node.jsframework~20 mins

CORS configuration in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CORS Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What does CORS stand for and why is it important?

Choose the correct explanation of CORS and its purpose in web development.

ACross-Origin Resource Sharing; it allows web pages to request resources from different domains securely.
BCross-Origin Request Security; it blocks all requests from other domains by default.
CCross-Origin Resource Sharing; it disables all security checks for requests between domains.
DCross-Origin Request Sharing; it encrypts data sent between different domains.
Attempts:
2 left
💡 Hint

Think about how browsers handle requests from one website to another domain.

component_behavior
intermediate
2:00remaining
What is the output when this Node.js Express server handles a cross-origin request?

Given the following Express server code with CORS configured, what will the browser receive when a request comes from http://example.com?

Node.js
import express from 'express';
import cors from 'cors';

const app = express();

app.use(cors({ origin: 'http://example.com' }));

app.get('/', (req, res) => {
  res.json({ message: 'Hello World' });
});

app.listen(3000);
AThe browser receives the JSON response but without any CORS headers, so access is blocked.
BThe server throws an error because the origin is hardcoded.
CThe browser receives a 404 Not Found error.
DThe browser receives the JSON response with CORS headers allowing access from http://example.com.
Attempts:
2 left
💡 Hint

Check how the cors middleware is configured and what it does.

📝 Syntax
advanced
2:00remaining
Which option correctly configures CORS to allow multiple specific origins in Express?

Choose the code snippet that correctly allows CORS requests from http://site1.com and http://site2.com only.

Aapp.use(cors({ origin: '*http://site1.com*|*http://site2.com*' }));
Bapp.use(cors({ origin: ['http://site1.com', 'http://site2.com'] }));
Capp.use(cors({ origin: function(origin, callback) { if(['http://site1.com', 'http://site2.com'].includes(origin)) callback(null, true); else callback(new Error('Not allowed')); } }));
Dapp.use(cors({ origin: 'http://site1.com,http://site2.com' }));
Attempts:
2 left
💡 Hint

The cors package supports arrays for the origin option.

🔧 Debug
advanced
2:00remaining
Why does this CORS configuration cause a runtime error?

Examine the code below. Why does the server crash when starting?

Node.js
import express from 'express';
import cors from 'cors';

const app = express();

app.use(cors({ origin: true }));

app.listen(3000);
AThe server crashes because the port 3000 is already in use.
BThe server crashes because the cors package is not imported correctly.
CThe origin option set to true is valid and does not cause a crash.
DThe value true for origin is invalid and causes a TypeError at runtime.
Attempts:
2 left
💡 Hint

Check the documentation for the cors package about the origin option.

state_output
expert
2:00remaining
What is the value of the Access-Control-Allow-Origin header for this request?

Given this Express server code and a request from http://evil.com, what will be the value of the Access-Control-Allow-Origin header in the response?

Node.js
import express from 'express';
import cors from 'cors';

const allowedOrigins = ['http://good.com', 'http://friend.com'];

const app = express();

app.use(cors({
  origin: (origin, callback) => {
    if (!origin) return callback(null, true);
    if (allowedOrigins.includes(origin)) {
      callback(null, origin);
    } else {
      callback(null, false);
    }
  }
}));

app.get('/', (req, res) => {
  res.send('Hello');
});

app.listen(3000);
Aundefined (no Access-Control-Allow-Origin header)
B"*"
C"http://evil.com"
D"http://good.com"
Attempts:
2 left
💡 Hint

Consider what happens when the origin is not in the allowed list and how the callback is called.