0
0
Expressframework~20 mins

What is Express - Practice Questions & Exercises

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

Express is a popular framework in Node.js. What is its main purpose?

ATo style web pages with CSS
BTo manage databases and perform SQL queries
CTo create web servers and handle HTTP requests easily
DTo compile JavaScript into machine code
Attempts:
2 left
💡 Hint

Think about what web servers do and how Express helps with that.

component_behavior
intermediate
2:00remaining
What does this Express code output when accessed at '/'?

Consider this Express app code. What will a user see when they visit the root URL '/'?

Express
import express from 'express';
const app = express();
app.get('/', (req, res) => {
  res.send('Hello from Express!');
});
app.listen(3000);
AThe browser shows the text: Hello from Express!
BThe browser shows a 404 Not Found error
CThe server crashes with an error
DThe browser shows a blank page
Attempts:
2 left
💡 Hint

Look at the route defined with app.get and what it sends back.

📝 Syntax
advanced
2:00remaining
Which option correctly creates an Express app and starts listening on port 4000?

Choose the code snippet that correctly creates an Express app and starts the server on port 4000 without errors.

A
const express = require('express')
const app = express
app.listen(4000)
B
import express from 'express'
const app = express()
app.listen(4000, () => console.log('Server running'))
C
import express from 'express'
const app = express
app.listen(4000, () => console.log('Server running'))
D
const express = require('express')
const app = express()
app.listen(4000, () => console.log('Server running'))
Attempts:
2 left
💡 Hint

Remember to call the express function to create the app and use correct import syntax for ES modules.

🔧 Debug
advanced
2:00remaining
Why does this Express app crash with 'TypeError: app.get is not a function'?

Look at this code snippet. Why does it crash with the error 'TypeError: app.get is not a function'?

Express
import express from 'express';
const app = express();
app.get('/', (req, res) => res.send('Hi'));
app.listen(3000);
ABecause express is not called as a function to create the app instance
BBecause the route '/' is missing a callback function
CBecause app.listen is missing a port number
DBecause import syntax is incorrect
Attempts:
2 left
💡 Hint

Check how the app variable is assigned from express.

state_output
expert
3:00remaining
What is the output after these Express middleware run in order?

Given this Express app with middleware, what will the client see when requesting '/'?

Express
import express from 'express';
const app = express();

app.use((req, res, next) => {
  req.message = 'Hello';
  next();
});

app.use((req, res, next) => {
  req.message += ' from middleware';
  next();
});

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

app.listen(3000);
AError: req.message is undefined
BHello
Cfrom middleware
DHello from middleware
Attempts:
2 left
💡 Hint

Think about how middleware modifies the request object before the route handler runs.