express-before-next
v1.0.1
Published
Clean up between Express routers.
Downloads
6
Readme
Express Before Next
Combining several Express apps and routers together is useful, but often changes to the request and response are leaked down the router chain. This plugin provides a small API for cleaning up when the next()
method is called on the router.
Install
npm install express-before-next
Usage
Call attachBeforeNext(router)
on any Express Router or Application instance. This will monkey patch the .handle()
method on these objects. A req.beforeNext()
is attached to the request, pass it functions to be called on clean up. Keep in mind this method are only called if next is called.
const express = require("express");
const attachBeforeNext = require("express-before-next");
function createHomeRoute() {
const router = express();
// monkey patch this router
attachBeforeNext(router);
router.use(function(req, res, next) {
// set state on the request that should be cleaned up
res.locals.foo = "bar";
// clean up state before going to the next router
req.beforeNext(() => {
delete res.locals.foo;
});
});
router.get("/", function(req, res) {
res.render("home");
});
return router;
}
const bigapp = express();
bigapp.use(createHomeRouter());
// when these routes get called, res.locals won't bleed in from above
bigapp.use(otherRoutes());