Complete the code to get user input from the request body in Express.
app.post('/submit', (req, res) => { const userInput = req.body.[1]; res.send('Received'); });
The user input is usually accessed from req.body using the key that matches the form field name, here 'input'.
Complete the code to check if the user input is empty and respond with an error.
app.post('/submit', (req, res) => { const input = req.body.input; if (input [1] '') { return res.status(400).send('Input is required'); } res.send('Input received'); });
We check if the input is exactly equal to an empty string to detect missing input.
Fix the error in the input validation to ensure only numbers are accepted.
app.post('/submit', (req, res) => { const input = req.body.input; if (!Number.isInteger([1])) { return res.status(400).send('Input must be an integer'); } res.send('Valid input'); });
Since req.body.input is typically a string, convert it to a number using Number(input) first. Then Number.isInteger() checks if it's an integer.
Fill both blanks to sanitize and validate user input as a trimmed non-empty string.
app.post('/submit', (req, res) => { const input = req.body.input[1](); if (input [2] '') { return res.status(400).send('Input cannot be empty'); } res.send('Input accepted'); });
Using trim() removes spaces around the input. Then checking if it is exactly equal to empty string detects empty input after trimming.
Fill all three blanks to validate input length and type before processing.
app.post('/submit', (req, res) => { const input = req.body.input; if (typeof input !== [1] || input.length [2] 0 || input.length [3] 100) { return res.status(400).send('Invalid input'); } res.send('Input is valid'); });
We check that input is a string, its length is greater than 0, and length is less than or equal to 100 to ensure valid input size.