0
0
Expressframework~10 mins

Why API documentation 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 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'
Aexpress
B'3000'
Capp
D3000
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.
2fill in blank
medium

Complete 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'
AbodyParser.json
BjsonParser
Cexpress.json
Dexpress.body
Attempts:
3 left
💡 Hint
Common Mistakes
Using bodyParser.json without importing body-parser.
Using incorrect middleware names.
3fill in blank
hard

Fix 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'
AsendText
Bjson
CsendFile
DsendHtml
Attempts:
3 left
💡 Hint
Common Mistakes
Using res.sendText or res.sendHtml which are not valid Express methods.
Using res.sendFile for JSON data.
4fill in blank
hard

Fill 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'
Apost
Bbody
Cget
Dparams
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.
5fill in blank
hard

Fill 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'
Aget
Bjson
C3000
Dpost
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.