0
0
Expressframework~10 mins

Why production setup 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 start an Express server listening on port 3000.

Express
const express = require('express');
const app = express();
app.listen([1], () => {
  console.log('Server running');
});
Drag options to blanks, or click blank then click option'
A3000
Bapp
C'3000'
Dexpress
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 add a middleware that parses JSON bodies in requests.

Express
const express = require('express');
const app = express();
app.use([1]());
Drag options to blanks, or click blank then click option'
AbodyParser.json
Bexpress.json
CjsonParser
Dexpress.body
Attempts:
3 left
💡 Hint
Common Mistakes
Using bodyParser.json without importing body-parser.
Using incorrect method names like express.body.
3fill in blank
hard

Fix the error in the code to properly handle errors in Express.

Express
app.use((err, req, res, [1]) => {
  console.error(err.stack);
  res.status(500).send('Something broke!');
});
Drag options to blanks, or click blank then click option'
Anext
Berror
Cresponse
Drequest
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter names like 'error' or 'response' instead of 'next'.
Missing the fourth parameter entirely.
4fill in blank
hard

Fill both blanks to create a route that responds with JSON and sets a custom header.

Express
app.get('/info', (req, res) => {
  res.set('[1]', '[2]').json({ message: 'Hello' });
});
Drag options to blanks, or click blank then click option'
AX-Custom-Header
BContent-Type
CCustomValue
Dapplication/json
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Content-Type' header incorrectly here.
Not chaining the methods properly.
5fill in blank
hard

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

Express
app.use(([1], [2], [3]) => {
  console.log(`$[1].method} $[2].url}`);
  [3]();
});
Drag options to blanks, or click blank then click option'
Areq
Bres
Cnext
Drequest
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'request' instead of 'req'.
Forgetting to call next() causing the request to hang.