Socket
Socket
Sign inDemoInstall

express-unless

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-unless

Conditionally add a middleware to express with some common patterns.


Version published
Maintainers
1
Created

What is express-unless?

The express-unless package is a middleware for Express.js that allows you to conditionally skip other middleware based on certain criteria. This is useful for scenarios where you want to apply middleware to most routes but exclude certain ones based on conditions like the request path, HTTP method, or custom logic.

What are express-unless's main functionalities?

Path-based exclusion

This feature allows you to exclude certain paths from the middleware. In this example, the middleware is applied to all routes except '/public' and '/about'.

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

const myMiddleware = (req, res, next) => {
  // Middleware logic
  next();
};

myMiddleware.unless = unless;

app.use(myMiddleware.unless({ path: ['/public', '/about'] }));

app.get('/public', (req, res) => res.send('Public Page'));
app.get('/about', (req, res) => res.send('About Page'));
app.get('/private', (req, res) => res.send('Private Page'));

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

Method-based exclusion

This feature allows you to exclude certain HTTP methods from the middleware. In this example, the middleware is applied to all routes except those using the GET and POST methods.

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

const myMiddleware = (req, res, next) => {
  // Middleware logic
  next();
};

myMiddleware.unless = unless;

app.use(myMiddleware.unless({ method: ['GET', 'POST'] }));

app.get('/test', (req, res) => res.send('GET request'));
app.post('/test', (req, res) => res.send('POST request'));
app.put('/test', (req, res) => res.send('PUT request'));

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

Custom logic exclusion

This feature allows you to exclude middleware based on custom logic. In this example, the middleware is skipped if the request contains a header 'x-custom-header' with the value 'skip'.

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

const myMiddleware = (req, res, next) => {
  // Middleware logic
  next();
};

myMiddleware.unless = unless;

app.use(myMiddleware.unless((req) => {
  return req.headers['x-custom-header'] === 'skip';
}));

app.get('/test', (req, res) => res.send('Test route'));

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

Other packages similar to express-unless

FAQs

Package last updated on 03 Aug 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc