0
0
Expressframework~20 mins

Why Express for Node.js web servers - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why is Express popular for Node.js web servers?

Which of the following best explains why Express is widely used for building web servers in Node.js?

AExpress provides a minimal and flexible framework that simplifies routing and middleware management.
BExpress automatically compiles Node.js code into faster machine code for better performance.
CExpress replaces Node.js core modules with its own proprietary modules for security.
DExpress is a graphical user interface tool for designing web server layouts visually.
Attempts:
2 left
💡 Hint

Think about what Express adds on top of Node.js to make web server coding easier.

component_behavior
intermediate
2:00remaining
What happens when you add middleware in Express?

Consider this Express code snippet adding middleware. What is the behavior when a request matches the middleware?

app.use((req, res, next) => {
  console.log('Middleware called');
  next();
});
AThe middleware causes a syntax error because next() is missing parentheses.
BThe message logs and the request ends immediately without reaching any route handlers.
CThe message 'Middleware called' logs, then the request continues to the next middleware or route handler.
DThe middleware blocks all requests and returns a 404 error automatically.
Attempts:
2 left
💡 Hint

What does calling next() inside middleware do?

📝 Syntax
advanced
2:00remaining
Identify the correct Express route syntax

Which option shows the correct syntax to define a GET route for path '/hello' that sends 'Hi!' as response?

Aapp.get('/hello', (req, res) => { res.send('Hi!'); });
Bapp.route('/hello').get = (req, res) => { res.send('Hi!'); };
Capp.get('/hello', (request, response) => response.write('Hi!'));
Dapp.get('/hello', (req, res) => { res.write('Hi!'); res.end(); });
Attempts:
2 left
💡 Hint

Remember the Express method to send a response body in one call.

state_output
advanced
2:00remaining
What is the output when multiple middleware modify res.locals?

Given this Express code, what will be the final response sent?

app.use((req, res, next) => {
  res.locals.value = 1;
  next();
});
app.use((req, res, next) => {
  res.locals.value += 2;
  next();
});
app.get('/', (req, res) => {
  res.send(`Value is ${res.locals.value}`);
});
AValue is undefined
BValue is 1
CValue is 2
DValue is 3
Attempts:
2 left
💡 Hint

Think about how res.locals is shared across middleware in the same request.

🔧 Debug
expert
2:00remaining
Why does this Express app crash on startup?

Examine this Express app code. Why does it crash with a TypeError?

const express = require('express');
const app = express();

app.use(express.json());

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

app.listen(3000, () => {
  console.log('Server started on port 3000');
});
AThe app crashes because express.json() is not a valid middleware function.
BMiddleware app.use(express.json()) is added after app.listen, so it never runs for requests.
CThe app crashes because app.listen must be called before defining routes.
DThe app crashes because res.send() is called without ending the response.
Attempts:
2 left
💡 Hint

Consider the order of middleware and server start.