Express - A Web Framework for Node.js

  • Express is a fast, unopinionated, and minimalist web framework for Node.js.

  • It provides robust tools for creating server-side applications and APIs, handling HTTP requests and responses, and managing various other aspects of web application development.

Why Use Express?

  • Minimalist: Express is lightweight and does not come with unnecessary features, allowing developers to build applications with a small footprint.

  • Flexible: It provides a thin layer of fundamental web application features, without obscuring Node.js features.

  • Extensible: Numerous middleware modules are available to extend its functionality.

  • Performance: Built on top of the asynchronous, non-blocking Node.js runtime, Express can handle a large number of simultaneous connections efficiently.

Setting Up an Express Application

Initialize a Node.js Project:

npm init -y

Install Express:

npm install express

Basic Application Structure:

const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
    res.send('Hello World!');
});

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`);
});

Middleware in Express

  • Middleware functions are functions that have access to

    • the request object (req)

    • the response object (res)

    • the next middleware function in the application’s request-response cycle.

Middleware can:

  • Execute any code.

  • Make changes to the request and response objects.

  • End the request-response cycle.

  • Call the next middleware function.

Example of Middleware:

Routing in Express

  • Routing refers to how an application’s endpoints (URIs) respond to client requests.

  • Express provides a simple and flexible way to define routes.

Basic Routing Example:

Route Parameters:

Route parameters are named URL segments that capture the values specified at their position in the URL.

Handling Different HTTP Methods

Express can handle various HTTP methods such as GET, POST, PUT, DELETE, etc.

Example:

Serving Static Files

Express provides a built-in middleware to serve static files such as images, CSS files, and JavaScript files.

Example:

This will serve all files in the public directory at the root URL.

Error Handling

Error handling middleware functions have four arguments: (err, req, res, next).

Example:

Working with JSON and URL-encoded Data

Express has built-in middleware to handle JSON and URL-encoded data.

Example:

Example Application with Express

Here's a more complete example showcasing an Express application with multiple routes, middleware, error handling, and serving static files:

In this example: Middleware is used to log request details and serve static files from the public directory. The body-parser middleware is used to parse JSON and URL-encoded data. Routes are defined to handle GET and POST requests. An error-handling middleware is included to handle any errors that occur during request processing. This example showcases how Express simplifies the development of robust web applications with a clean and modular structure.

Last updated