express-middleware-select
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

express-middleware-select

Select a middleware to execute.

You must provide as the first parameter a number which is the index of the middleware to be executed in an array of middlewares passed as the second parameter.

import express from "express";
import { select } from "express-middleware-select";

const app = express();

app.use(
    select(
        function (req: Request) {
            if (req.headers["accept"] === "application/json") return 0;
            else return 1;
        },
        [
            function (req: Request, res: Response) {
                res.end("json");
            },
            function (req: Request, res: Response) {
                res.end("text");
            },
        ]
    )
);

You can also use it by passing a number as first parameter:

app.use(
    select(1, [
        function (req: Request, res: Response) {
            res.end("json");
        },
        function (req: Request, res: Response) {
            res.end("text");
        },
    ])
);

You can also use it as CommonJS module.

const { select } = require("express-middleware-select");

const app = express();

app.use(
    select(
        function (req: Request) {
            if (req.headers["accept"] === "application/json") return 0;
            else return 1;
        },
        [
            function (req: Request, res: Response) {
                res.end("json");
            },
            function (req: Request, res: Response) {
                res.end("text");
            },
        ]
    )
);

Package Sidebar

Install

npm i express-middleware-select

Weekly Downloads

0

Version

0.0.1

License

MIT

Unpacked Size

10.7 kB

Total Files

6

Last publish

Collaborators

  • cedrick-ah