I just published a package for this purpose:
https://www.npmjs.com/package/express-switchware
Usage:
const stripeHandler: express.RequestHandler = (req, res) =>
res.send("Processed payment with Stripe");
const paypalHandler: express.RequestHandler = (req, res) =>
res.send("Processed payment with PayPal");
app.post(
"/checkout",
expressSwitchware(
(req) => req.body.provider, // e.g. { "provider": "stripe" }
{
stripe: stripeHandler,
paypal: paypalHandler,
}
)
);
I think it is simple and elegant.