Express Server and Routing
Express is a lightweight Node.js web framework that provides routing, middleware, request handling, and response helpers. It is commonly used to build REST APIs and web servers.
Routes map HTTP methods and URL patterns to handler functions.
Express REST Routes
import express from 'express';
const app = express();
app.use(express.json());
app.get('/users', (req, res) => {
res.json([{ id: 1, name: 'Suriya' }]);
});
app.post('/users', (req, res) => {
res.status(201).json(req.body);
});
app.listen(3000);💡 The application parses JSON request bodies and defines GET and POST endpoints.
Key Takeaways
- Express simplifies HTTP routing and middleware composition.
- Use routers to keep larger applications modular.
- Validate incoming request data instead of trusting client input.
Knowledge Check
Which Express middleware parses JSON request bodies?