0
0
Expressframework~20 mins

How Express builds on Node.js HTTP - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express HTTP Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Express middleware do?

Consider this Express middleware function:

app.use((req, res, next) => {
  console.log(req.method);
  next();
});

What will be printed when a GET request is made to the server?

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

app.use((req, res, next) => {
  console.log(req.method);
  next();
});

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

app.listen(3000);
Aundefined
BPOST
CGET
DError
Attempts:
2 left
💡 Hint

Look at the request method property logged.

📝 Syntax
intermediate
2:00remaining
Identify the correct way to create an Express server using Node.js HTTP module

Which option correctly creates an Express app and starts a server using Node.js HTTP module?

Express
const express = require('express');
const http = require('http');
const app = express();
Ahttp.createServer(app.listen(3000));
B
const server = http.createServer(app);
server.listen(3000);
C
app.createServer(http);
app.listen(3000);
D
const server = http.createServer();
server.listen(3000, app);
Attempts:
2 left
💡 Hint

Express apps can be passed as request listeners to Node.js HTTP servers.

🔧 Debug
advanced
2:00remaining
Why does this Express app not respond to requests?

Given this code, why does the server not respond to requests?

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

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

// Missing app.listen call
Express
const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hi');
});
AExpress requires a callback in app.get to be async
BThe route handler is incorrect and causes an error
CThe app needs to import http module to work
DThe server never starts listening because app.listen is missing
Attempts:
2 left
💡 Hint

Check if the server is actually listening for requests.

state_output
advanced
2:00remaining
What is the output of this Express app using Node.js HTTP server?

What will be the response body when a client requests '/'?

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

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

const server = http.createServer(app);
server.listen(3000);
AWelcome
BCannot GET /
CError: app is not a function
DEmpty response
Attempts:
2 left
💡 Hint

Express app can be used as a request handler for Node.js HTTP server.

🧠 Conceptual
expert
3:00remaining
How does Express extend Node.js HTTP request and response objects?

Which statement best describes how Express builds on Node.js HTTP module?

AExpress wraps Node.js HTTP request and response objects to add helper methods like res.send and req.params
BExpress replaces Node.js HTTP module with its own implementation
CExpress uses a different protocol than HTTP for requests and responses
DExpress requires manual parsing of request body unlike Node.js HTTP
Attempts:
2 left
💡 Hint

Think about how Express adds convenience methods to the request and response.