Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the Express module.
Express
const express = require([1]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using other module names like 'http' or 'fs' instead of 'express'.
Forgetting to put quotes around the module name.
✗ Incorrect
To use Express, you must import it with require('express').
2fill in blank
mediumComplete the code to create a new Express application.
Express
const app = [1](); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call
require() instead of express().Using
http() which is not the Express app creator.✗ Incorrect
You create an Express app by calling the express() function.
3fill in blank
hardFix the error in the GET route handler to send a response.
Express
app.get('/hello', (req, res) => { res.[1]('Hello World'); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
res.write() without ending the response.Using
res.end() without sending data.✗ Incorrect
The res.send() method sends the response body to the client.
4fill in blank
hardFill both blanks to start the server on port 3000 and log a message.
Express
app.listen([1], () => { console.[2]('Server running on port 3000'); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
console.error or console.info instead of console.log.Passing a string instead of a number as the port.
✗ Incorrect
The app.listen method starts the server on the given port. console.log prints the message.
5fill in blank
hardFill all three blanks to test the GET /hello endpoint using supertest.
Express
import request from 'supertest'; import app from './app'; test('GET /hello returns Hello World', async () => { const response = await request(app).[1]('/hello'); expect(response.statusCode).toBe([2]); expect(response.[3]).toBe('Hello World'); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
post instead of get.Checking
response.text instead of response.body.Expecting status code other than 200.
✗ Incorrect
Use request(app).get('/hello') to send a GET request. The status code for success is 200. The response body contains the returned string.