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 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 or express object instead of a port number.
✗ Incorrect
The listen method requires a port number as a number, not a string or other value.
2fill in blank
mediumComplete the code to parse JSON request bodies in Express.
Express
const express = require('express'); const app = express(); app.use([1]());
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using bodyParser.json without importing body-parser.
Using incorrect middleware names.
✗ Incorrect
Express has a built-in json middleware accessed by express.json().
3fill in blank
hardFix the error in the route handler to send a JSON response.
Express
app.get('/data', (req, res) => { res.[1]({ message: 'Hello API' }); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using res.sendText or res.sendHtml which are not valid Express methods.
Using res.sendFile for JSON data.
✗ Incorrect
To send JSON data, use res.json() method.
4fill in blank
hardFill both blanks to create a route that accepts POST requests and reads JSON data.
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 app.get instead of app.post for POST routes.
Accessing req.params instead of req.body for JSON data.
✗ Incorrect
POST routes use app.post and JSON data is in req.body.
5fill in blank
hardFill all three blanks to create a simple Express API with a GET route that returns a JSON message.
Express
const express = require('express'); const app = express(); app.[1]('/hello', (req, res) => { res.[2]({ message: 'Hi there!' }); }); app.listen([3], () => console.log('API running'));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using app.post for a GET route.
Using res.send instead of res.json for JSON response.
Passing a string instead of a number for the port.
✗ Incorrect
GET route uses app.get, sends JSON with res.json, and listens on port 3000.