What is Express.js
What is Express.js?
Express.js is a web application framework for Node.js, designed to simplify the process of building web applications and APIs. It provides a robust set of features for building web and mobile applications, and is known for its minimalist, unopinionated approach, giving developers flexibility while providing essential tools and utilities.
Key Features of Express.js
1. Middleware Support
Express.js uses a concept called middleware to handle HTTP requests. Middleware functions are executed sequentially and can perform various tasks such as:
- Request logging
- Request body parsing
- Authentication
- Error handling
Middleware functions can modify the request and response objects, end the request-response cycle, or call the next middleware in the stack.
Example:
app.use(express.json()); // Middleware to parse JSON request bodies
2. Routing
Express.js provides a flexible routing system to handle different HTTP methods (GET, POST, PUT, DELETE, etc.) and URL paths.
Example:
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.post('/submit', (req, res) => {
res.send('Form submitted!');
});
3. Static File Serving
Express.js can serve static files such as images, CSS, and JavaScript from a directory.
Example:
app.use(express.static('public')); // Serves files from the 'public' directory
4. Template Engine Integration
Express.js supports integration with various template engines like EJS, Pug, and Handlebars, allowing you to generate dynamic HTML pages.
Example:
app.set('view engine', 'ejs');
app.get('/hello', (req, res) => {
res.render('hello', { name: 'World' });
});
5. Error Handling
Express.js provides mechanisms for handling errors and sending appropriate responses to clients.
Example:
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Something went wrong!');
});
6. Asynchronous Support
Express.js fully supports asynchronous operations, making it suitable for handling I/O operations such as database queries and file handling.
Example:
app.get('/data', async (req, res) => {
const data = await fetchDataFromDatabase();
res.json(data);
});
Basic Example
Here's a simple Express.js application that sets up a server and defines a couple of routes:
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.get('/about', (req, res) => {
res.send('About Page');
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
This code creates a server that listens on port 3000 and responds to requests at the root (/
) and /about
endpoints.
Summary
- Purpose: Express.js simplifies the development of web applications and APIs in Node.js.
- Features:
- Middleware Support: For handling requests and responses.
- Routing: Flexible URL and HTTP method handling.
- Static File Serving: Serve static files.
- Template Engine Integration: Render dynamic HTML.
- Error Handling: Manage and respond to errors.
- Asynchronous Support: Handle async operations effectively.
Express.js is a foundational tool for building web applications and services in the Node.js ecosystem, known for its simplicity and flexibility.