0
0
Node.jsframework~10 mins

Why security is critical in Node.js - Test Your Understanding

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

Complete the code to import the built-in Node.js module for handling HTTPS requests.

Node.js
const https = require('[1]');
Drag options to blanks, or click blank then click option'
Afs
Bhttp
Curl
Dhttps
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'http' instead of 'https' which is not secure.
Confusing 'fs' (file system) with network modules.
2fill in blank
medium

Complete the code to create a secure HTTPS server using Node.js.

Node.js
const server = https.createServer({ key: privateKey, cert: certificate }, [1]);
Drag options to blanks, or click blank then click option'
AhandleRequest
BrequestListener
CcreateServer
Dlisten
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the wrong function name or method.
Confusing server creation with listening.
3fill in blank
hard

Fix the error in the code to properly handle user input securely.

Node.js
app.post('/login', (req, res) => {
  const username = req.body.[1];
  // process login
});
Drag options to blanks, or click blank then click option'
Aname
Buser
Cusername
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect property names that do not exist in the request body.
Not validating or sanitizing user input.
4fill in blank
hard

Fill both blanks to create a middleware that protects routes by checking authentication.

Node.js
function authMiddleware(req, res, [1]) {
  if (!req.user) {
    return res.status([2]).send('Unauthorized');
  }
  next();
}
Drag options to blanks, or click blank then click option'
Anext
B401
C403
Derror
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter names in middleware.
Sending incorrect HTTP status codes.
5fill in blank
hard

Fill all three blanks to safely parse JSON input and handle errors in a Node.js server.

Node.js
app.use(express.text());

app.post('/data', (req, res) => {
  try {
    const data = JSON.[1](req.body);
    res.send([2]);
  } catch ([3]) {
    res.status(400).send('Invalid JSON');
  }
});
Drag options to blanks, or click blank then click option'
Aparse
Bdata
Cerror
Dstringify
Attempts:
3 left
💡 Hint
Common Mistakes
Using JSON.stringify instead of JSON.parse for input.
Not catching errors properly.