Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import Express and create an app instance.
Express
const express = require('[1]'); const app = express();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'http' instead of 'express' for the import.
Forgetting to import express before creating the app.
✗ Incorrect
You need to import the 'express' module to create an Express app.
2fill in blank
mediumComplete the code to parse incoming JSON data in requests.
Express
app.use([1].json()); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using bodyParser without importing it.
Trying to use router.json() which doesn't exist.
✗ Incorrect
Express has a built-in JSON parser accessed via express.json().
3fill in blank
hardFix the error in the route handler to send a JSON response.
Express
app.post('/register', (req, res) => { const user = req.body; // Save user logic here res.[1]({ message: 'User registered successfully' }); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using res.sendFile() which expects a file path.
Using res.redirect() which sends a redirect response.
✗ Incorrect
Use res.json() to send a JSON response to the client.
4fill in blank
hardFill both blanks to create a route that handles GET requests for user data.
Express
app.[1]('/user/:id', (req, [2]) => { const userId = req.params.id; // Fetch user logic res.json({ id: userId }); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using app.post() instead of app.get() for GET routes.
Mixing up the order of req and res parameters.
✗ Incorrect
The route should use app.get() for GET requests, and the second parameter is the response object 'res'.
5fill in blank
hardFill all three blanks to start the server on port 3000 and log a message.
Express
const PORT = [1]; app.listen(PORT, () => { console.log('Server running on port [2]'); [3]; });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong port number.
Using console.error instead of console.log for normal messages.
✗ Incorrect
Use port 3000 for the server, log the port number, and print a ready message.