0
0
Expressframework~10 mins

Why architectural 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.

Express
const express = require('express');
const app = express();
app.get('/', (req, res) => {
  res.send('Hello World!');
});
app.listen([1], () => {
  console.log('Server is running');
});
Drag options to blanks, or click blank then click option'
A'3000'
B3000
Capp
Dexpress
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of a number for the port.
Passing the app object instead of a port number.
2fill in blank
medium

Complete the code to add middleware that logs request method and URL.

Express
app.use((req, res, next) => {
  console.log(req.[1] + ' ' + req.url);
  next();
});
Drag options to blanks, or click blank then click option'
Amethod
Bquery
Cbody
Dpath
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.path instead of req.method.
Trying to log req.body without parsing middleware.
3fill in blank
hard

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

Express
app.get('/data', (req, res) => {
  res.[1]({ message: 'Hello JSON' });
});
Drag options to blanks, or click blank then click option'
AsendText
BsendFile
Cjson
Drender
Attempts:
3 left
💡 Hint
Common Mistakes
Using sendFile when sending JSON data.
Using render without a view engine.
4fill in blank
hard

Fill both blanks to create a route that handles POST requests and parses JSON body.

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
Cquery
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using get instead of post for POST routes.
Accessing req.query instead of req.body.
5fill in blank
hard

Fill all three blanks to create a modular router and use it in the main app.

Express
const express = require('express');
const app = express();
const router = express.Router();

router.[1]('/info', (req, res) => {
  res.json({ info: 'Router info' });
});

app.[2]('/api', router);

app.listen([3], () => {
  console.log('Server running');
});
Drag options to blanks, or click blank then click option'
Aget
Buse
C3001
Dpost
Attempts:
3 left
💡 Hint
Common Mistakes
Using post instead of get for the router route.
Using app.get instead of app.use to mount router.
Passing a string instead of a number to listen.