To create a RESTful API with Express, you define routes for different HTTP methods (GET, POST, PUT, DELETE) and use middleware to handle requests. Example:
const express = require('express');
const app = express();
app.use(express.json());
app.get('/api/users', (req, res) => {
// Fetch and return users
res.json([{ id: 1, name: 'John Doe' }]);
});
app.post('/api/users', (req, res) => {
// Create a new user
res.status(201).json(req.body);
});
app.listen(3000, () => console.log('API running on http://localhost:3000'));