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 server listens on port 3000, which is a common default port for Express apps.
Complete the code to parse incoming JSON request 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 JSON request bodies so you can access them via req.body.
Fix the error in the route handler to correctly send a 404 status when a resource is not found.
app.get('/item/:id', (req, res) => { const item = database.find(i => i.id === req.params.id); if (!item) { res.status([1]).send('Not Found'); } else { res.json(item); } });
HTTP status 404 means the requested resource was not found, which is appropriate here.
Fill both blanks to create a RESTful route that updates an item by ID using the correct HTTP method and URL pattern.
app.[1]('/items/[2]', (req, res) => { // update item logic here res.send('Item updated'); });
PUT is the HTTP method used to update a resource fully, and ':id' is the URL parameter for the item identifier.
Fill all three blanks to create a RESTful Express route that deletes an item by ID and sends a 204 status code.
app.[1]('/items/[2]', (req, res) => { // delete item logic res.status([3]).send(); });
DELETE is the HTTP method to remove a resource, ':itemId' is the URL parameter, and 204 means No Content, indicating successful deletion without response body.