Complete the code to create a basic 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 is running'); });
The number 3000 is the common default port for Express servers in development.
Complete the code to add middleware that parses JSON bodies in Express.
const express = require('express'); const app = express(); app.use([1]); app.post('/data', (req, res) => { res.json(req.body); });
express.json() middleware parses incoming JSON requests and puts the parsed data in req.body.
Fix the error in the route handler to send a JSON response with a message.
app.get('/hello', (req, res) => { res.[1]({ message: 'Hi there!' }); });
res.json() sends a JSON response. Other methods serve files, render views, or redirect.
Fill both blanks to create a route that responds to PUT requests at '/update'.
app.[1]('/update', (req, res) => { res.status([2]).send('Update received'); });
PUT is the HTTP method for update actions. Status 200 means success.
Fill all three blanks to create a middleware that logs the request method and URL, then calls next().
app.use((req, res, [1]) => { console.log(req.[2] + ' ' + req.[3]); next(); });
Middleware functions receive next to pass control. req.method and req.url give request info.