0
0
Expressframework~10 mins

Why advanced patterns matter 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 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'
A80
B8080
C5000
D3000
Attempts:
3 left
💡 Hint
Common Mistakes
Using a port number that is already in use by another service.
Leaving the port number blank or undefined.
2fill in blank
medium

Complete the code to add middleware that parses JSON bodies in Express.

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 bodyParser.json without importing body-parser.
Using incorrect middleware names.
3fill in blank
hard

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

Express
app.get('/user', (req, res) => {
  res.[1]({ name: 'Alice' });
});
Drag options to blanks, or click blank then click option'
Ajson
Brender
CsendFile
DsendText
Attempts:
3 left
💡 Hint
Common Mistakes
Using res.sendFile for JSON data.
Using res.render without a template engine.
4fill in blank
hard

Fill both blanks to create a route that handles URL parameters and sends them back.

Express
app.get('/user/:[1]', (req, res) => {
  const [2] = req.params.id;
  res.send(`User ID is ${id}`);
});
Drag options to blanks, or click blank then click option'
Aid
BuserId
Dparam
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching parameter names and variable names.
Using undefined variables in the response.
5fill in blank
hard

Fill all three blanks to create a middleware that logs request method and URL, then passes control.

Express
function logger(req, res, [1]) {
  console.log(`${req.method} ${req.[2]`);
  [3]();
}
app.use(logger);
Drag options to blanks, or click blank then click option'
Anext
Burl
DrequestUrl
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to call next() causing the request to hang.
Using incorrect parameter names.