0
0
Expressframework~10 mins

Why Express for Node.js web servers - 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 is running');
});
Drag options to blanks, or click blank then click option'
A8080
B5000
C3000
D80
Attempts:
3 left
💡 Hint
Common Mistakes
Using port 80 which requires admin rights
Using a string instead of a number
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()
Bexpress.urlencoded({ extended: true })
Cexpress.static('public')
Dexpress.raw()
Attempts:
3 left
💡 Hint
Common Mistakes
Using urlencoded middleware for JSON
Using static middleware which serves files
3fill in blank
hard

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

Express
app.get('/hello', (req, res) => {
  res.[1]({ message: 'Hi there!' });
});
Drag options to blanks, or click blank then click option'
AsendFile
Bjson
Crender
Dredirect
Attempts:
3 left
💡 Hint
Common Mistakes
Using sendFile which expects a file path
Using render without a view engine
4fill in blank
hard

Fill both blanks to create a route that responds to PUT requests at '/update'.

Express
app.[1]('/update', (req, res) => {
  res.status([2]).send('Update received');
});
Drag options to blanks, or click blank then click option'
Aput
Bpost
Cget
D200
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET or POST instead of PUT
Using status codes like 404 or 500
5fill in blank
hard

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

Express
app.use((req, res, [1]) => {
  console.log(req.[2] + ' ' + req.[3]);
  next();
});
Drag options to blanks, or click blank then click option'
Anext
Bmethod
Curl
Dsend
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to call next()
Using res.send instead of next
Logging wrong properties