Complete the code to define a simple GET endpoint that returns a welcome message.
app.get('/welcome', (req, res) => { res.[1]('Welcome to the API!'); });
The send method sends a response body as a string or buffer.
Complete the code to parse JSON data from a POST request body.
app.use([1]());express.json() middleware parses incoming JSON requests and puts the parsed data in req.body.
Fix the error in the route handler to correctly extract the user ID from the URL parameter.
app.get('/user/:id', (req, res) => { const userId = req.[1].id; res.send(`User ID is ${userId}`); });
URL parameters are accessed via req.params.
Fill both blanks to create a middleware that logs the HTTP method and URL of each request.
app.use((req, res, next) => {
console.log(req.[1] + ' ' + req.[2]);
next();
});req.method gives the HTTP method, and req.url gives the requested URL.
Fill all three blanks to create a route that responds with JSON containing the user's name and age if age is greater than 18.
app.post('/check', (req, res) => { const { name, age } = req.body; if (age [1] 18) { res.[2]({ [3] }); } else { res.status(403).send('Forbidden'); } });
The condition checks if age is greater than 18. The response sends JSON with name and age.