0
0
Expressframework~10 mins

Why testing APIs matters in Express - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a simple Express server that listens on port 3000.

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'
A5000
B8080
C3000
D80
Attempts:
3 left
💡 Hint
Common Mistakes
Using port 80 without admin rights
Using a string instead of a number
2fill in blank
medium

Complete the code to add JSON body parsing middleware to the Express app.

Express
const express = require('express');
const app = express();

app.use([1]());

app.post('/data', (req, res) => {
  res.json(req.body);
});
Drag options to blanks, or click blank then click option'
Aexpress.json
BbodyParser.json
CjsonParser
Dexpress.body
Attempts:
3 left
💡 Hint
Common Mistakes
Using deprecated body-parser package
Calling middleware without parentheses
3fill in blank
hard

Fix the error in the route handler to correctly send a JSON response with a message.

Express
app.get('/status', (req, res) => {
  res.[1]({ message: 'API is working' });
});
Drag options to blanks, or click blank then click option'
Ajson
BsendJson
CsendText
DsendMessage
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like sendJson
Using sendText which sends plain text
4fill in blank
hard

Fill both blanks to create a middleware that logs the HTTP method and URL of each request.

Express
app.use((req, res, next) => {
  console.log(req.[1] + ' ' + req.[2]);
  next();
});
Drag options to blanks, or click blank then click option'
Amethod
Burl
Cpath
Dbody
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.path instead of req.url
Logging req.body which is the request data
5fill in blank
hard

Fill all three blanks to create a route that responds with a status code 201 and a JSON message.

Express
app.post('/create', (req, res) => {
  res.status([1]).[2]([3]);
});
Drag options to blanks, or click blank then click option'
A201
Bjson
C{ message: 'Resource created' }
Dsend
Attempts:
3 left
💡 Hint
Common Mistakes
Using status code 200 instead of 201
Using send instead of json for JSON response