Complete the code to create a simple Express server that listens on port 3000.
const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send('Hello World!'); }); app.listen([1], () => { console.log('Server running'); });
The server listens on port 3000, which is a common default for Express apps.
Complete the code to add JSON body parsing middleware to the Express app.
const express = require('express'); const app = express(); app.use([1]()); app.post('/data', (req, res) => { res.json(req.body); });
Express has built-in JSON parsing middleware called express.json().
Fix the error in the route handler to correctly send a JSON response with a message.
app.get('/status', (req, res) => { res.[1]({ message: 'API is working' }); });
The res.json() method sends a JSON response.
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 a status code 201 and a JSON message.
app.post('/create', (req, res) => { res.status([1]).[2]([3]); });
The status code 201 means 'Created'. Use res.status(201).json({ message: 'Resource created' }) to send the response.