Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a basic Express server.
Express
const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send('Hello World!'); }); app.listen([1], () => { console.log('Server is running'); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of a number for the port.
Passing the app object instead of a port number.
✗ Incorrect
The listen method needs a port number as a number, not a string or other value.
2fill in blank
mediumComplete the code to add middleware that logs request method and URL.
Express
app.use((req, res, next) => {
console.log(req.[1] + ' ' + req.url);
next();
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
req.path instead of req.method.Trying to log
req.body without parsing middleware.✗ Incorrect
The method property of the request object tells us the HTTP method like GET or POST.
3fill in blank
hardFix the error in the route handler to send JSON response.
Express
app.get('/data', (req, res) => { res.[1]({ message: 'Hello JSON' }); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
sendFile when sending JSON data.Using
render without a view engine.✗ Incorrect
The json method sends a JSON response properly formatted.
4fill in blank
hardFill both blanks to create a route that handles POST requests and parses JSON body.
Express
app.[1]('/submit', (req, res) => { const data = req.[2]; res.json({ received: data }); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
get instead of post for POST routes.Accessing
req.query instead of req.body.✗ Incorrect
Use post to handle POST requests and req.body to access parsed JSON data.
5fill in blank
hardFill all three blanks to create a modular router and use it in the main app.
Express
const express = require('express'); const app = express(); const router = express.Router(); router.[1]('/info', (req, res) => { res.json({ info: 'Router info' }); }); app.[2]('/api', router); app.listen([3], () => { console.log('Server running'); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
post instead of get for the router route.Using
app.get instead of app.use to mount router.Passing a string instead of a number to
listen.✗ Incorrect
Use get to define a GET route on the router, use to mount the router on the app, and a port number like 3001 to listen.