Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a basic Express app.
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' to create the app
Forgetting to require the express module
✗ Incorrect
The express module is required to create an Express app.
2fill in blank
mediumComplete the code to start the Express server on port 3000.
Express
app.listen([1], () => { console.log('Server running on port 3000'); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a port number different from 3000 without updating the message
Passing a string instead of a number
✗ Incorrect
Port 3000 is commonly used for local Express servers.
3fill in blank
hardFix the error in the route handler to send 'Hello World' as response.
Express
app.get('/', (req, res) => { res.[1]('Hello World'); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Node.js response methods like write or end instead of Express's send
Forgetting to send a response
✗ Incorrect
The send method sends the response body in Express.
4fill in blank
hardFill both blanks to create a JSON response with Express.
Express
app.get('/data', (req, res) => { res.[1]({ message: 'Hello' }); res.[2](); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling send after json which can cause errors
Not ending the response
✗ Incorrect
Use json to send JSON data and end to finish the response.
5fill in blank
hardFill all three blanks to create a middleware that logs request method and URL.
Express
app.use(([1], [2], [3]) => { console.log(`$[1].method $[2].url`); next(); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter names
Forgetting to call next() to continue
✗ Incorrect
Middleware functions receive req, res, and next as parameters.