0
0
Node.jsframework~10 mins

Request validation in Node.js - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the Express library.

Node.js
const express = require('[1]');
Drag options to blanks, or click blank then click option'
Afs
Bhttp
Cexpress
Dpath
Attempts:
3 left
💡 Hint
Common Mistakes
Using built-in modules like 'http' instead of 'express'.
2fill in blank
medium

Complete the code to create a new Express application instance.

Node.js
const app = [1]();
Drag options to blanks, or click blank then click option'
Aexpress
Bhttp
Crouter
Dserver
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a non-existent function like 'http()'.
3fill in blank
hard

Fix the error in the middleware function to correctly parse JSON requests.

Node.js
app.use(express.[1]());
Drag options to blanks, or click blank then click option'
Ajson
Burlencoded
Cstatic
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'urlencoded' when expecting JSON data.
4fill in blank
hard

Fill both blanks to validate that the request body has a 'name' property before proceeding.

Node.js
app.post('/submit', (req, res, next) => {
  if (!req.body.[1]) {
    return res.status([2]).send('Name is required');
  }
  next();
});
Drag options to blanks, or click blank then click option'
Aname
B400
C404
Dusername
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for 'username' instead of 'name'.
Using 404 instead of 400 status code.
5fill in blank
hard

Fill all three blanks to create a middleware that validates 'email' and 'password' fields in the request body.

Node.js
function validateUser(req, res, next) {
  const { [1], [2] } = req.body;
  if (![1] || ![2]) {
    return res.status(400).send('Email and password are required');
  }
  next();
}
Drag options to blanks, or click blank then click option'
Aemail
Bpassword
Cusername
Dtoken
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'username' or 'token' instead of 'email' and 'password'.