Express.js is a minimal and flexible web application framework for Node.js. It simplifies building APIs and web apps by providing a robust set of features with minimal overhead.
Why use Express?
- Fast setup for REST APIs.
- Works seamlessly with middleware.
- Great for single-page apps (SPAs), mobile backends, and microservices.
Getting started:
- Install Express: bashКопироватьРедактировать
npm install express
- Create a basic server: jsКопироватьРедактировать
const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send('Hello World!'); }); app.listen(3000, () => { console.log('Server running on port 3000'); });
Core concepts:
- Routing: Handle GET, POST, PUT, DELETE requests.
- Middleware: Functions that modify requests/responses (e.g., authentication, logging).
- Static files: Serve HTML, CSS, and JS from a public folder.
Popular middleware:
body-parser
for JSON parsing.cors
for cross-origin resource sharing.morgan
for logging.
Express is simple yet powerful—ideal for both beginners and professionals building scalable APIs or full-stack apps (especially with MongoDB and React or Angular).