0
0
Expressframework~20 mins

Creating a basic Express server - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express Server Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when accessing the root URL?
Consider this Express server code. What will the server respond with when you visit http://localhost:3000/ in a browser?
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 server crashes with an error
CThe browser shows a blank page with status 200
DThe browser shows a 404 Not Found error
Attempts:
2 left
💡 Hint
Look at the route defined with app.get and what it sends back.
📝 Syntax
intermediate
2:00remaining
Which option correctly creates an Express server listening on port 8080?
Choose the code snippet that correctly starts an Express server on port 8080.
A
const app = express();
app.listen(8080, () => console.log('Server running on 8080'));
B
const app = express;
app.listen(8080);
C
const app = express();
app.listen(8080); console.log('Server running on 8080');
D
const app = express();
app.start(8080);
Attempts:
2 left
💡 Hint
Remember to call express as a function and use the correct method to start the server.
🔧 Debug
advanced
2:00remaining
Why does this Express server code crash?
This code crashes when run. What is the cause of the error?
Express
import express from 'express';
const app = express();
app.get('/', (req, res) => {
  res.send('Welcome!');
});
app.listen();
Aexpress() must be called with a port number
Bapp.listen() is missing the port number argument
Cres.send() must be called with an object, not a string
Dapp.get() requires a second argument to be a named function
Attempts:
2 left
💡 Hint
Check the app.listen() method usage.
state_output
advanced
2:00remaining
What is the response when accessing '/user/42'?
Given this Express route, what will the server respond with when visiting /user/42?
Express
import express from 'express';
const app = express();
app.get('/user/:id', (req, res) => {
  res.send(`User ID is ${req.params.id}`);
});
app.listen(3000);
A404 Not Found error
BUser ID is undefined
CUser ID is :id
DUser ID is 42
Attempts:
2 left
💡 Hint
Look at how route parameters are accessed in Express.
🧠 Conceptual
expert
2:00remaining
What happens if you call app.listen() twice in the same Express app?
Consider an Express app where app.listen(3000) is called, and later app.listen(4000) is called again. What is the expected behavior?
AThe app stops listening on 3000 and only listens on 4000
BThe app listens on both ports 3000 and 4000 simultaneously
CThe second call throws an error because the app is already listening
DThe app ignores the second call and continues listening on 3000
Attempts:
2 left
💡 Hint
Think about how Node.js servers handle multiple listen calls on the same app instance.