Get a Quote Right Now

Edit Template

Middleware & Configurable middleware

If you need your middleware to be configurable, export a function which accepts an options object or other parameters, which, then returns the middleware implementation based on the input parameters.

Middleware in Express:

Middleware in Express are functions that have access to the request (req), response (res), and the next function in the application’s request-response cycle. They can modify the request and response objects, end the request-response cycle, or call the next middleware in the stack. Middleware is often used for tasks like logging, authentication, validation, and more.

Here’s a simple example of a middleware that logs the request method and URL:

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

// Custom middleware
app.use((req, res, next) => {
console.log(Received ${req.method} request at ${req.url});
next(); // Call the next middleware
});

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

app.listen(3000, () => {
console.log('Server is running on port 3000');
});

In this example, the custom middleware logs the request information before passing the control to the next middleware or route handler using the next() function.

Configurable Middleware in Express:

Configurable middleware allows you to create middleware functions that can accept parameters during their initialization. This enables you to create more flexible and reusable middleware that can be customized based on specific needs.

Here’s an example of configurable middleware that accepts a message parameter:

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

// Configurable middleware
const messageMiddleware = (message) => {
return (req, res, next) => {
console.log(message);
next();
};
};

app.use(messageMiddleware('This is a custom message'));

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

app.listen(3000, () => {
console.log('Server is running on port 3000');
});

In this example, the messageMiddleware function returns a middleware function that logs the custom message provided during its initialization. This allows you to create multiple instances of the same middleware with different messages.

You can further enhance configurable middleware by accepting additional parameters such as configuration objects, database connections, or authentication settings to make them even more versatile.

Both middleware and configurable middleware are powerful tools in Express that enable you to modularize your application’s logic, keep your codebase organized, and implement cross-cutting concerns effectively.

TYPES OF MIDDLEWARES:

These types of middleware are fundamental to Express applications, enabling you to handle various aspects of the request-response cycle efficiently and modularly.

  1. Application-level middleware
  2. Router-level middleware
  3. Error-handling middleware
  4. Built-in middleware
  5. Third-party middleware

Share

Leave a Reply

Your email address will not be published. Required fields are marked *