@tdsoft/express-routing-wrapper
v0.4.23
Published
Package gives you level of abstraction/wrapping of express router(s), endpoints and middlewares, additionally defining subrouters makes tree structure possible and easily discoverable by root router.
Downloads
8
Maintainers
Readme
##express-routing-wrapper Package gives you level of abstraction/wrapping of express router(s), endpoints and middlewares, additionally defining subrouters makes tree structure possible and easily discoverable by root router. Metadata passed to endpoints/middlewares are auto-validated (if required), whole module work by wrapping user-defined callback by higher level callback which first validate request, and throws descriptive errors to be caught by overlayed express middleware. All of this while keeping syntax as close as possible to original express routing.
Structure can help with things like generating docs, etc.
####Example const router = new ApiRouter("/api"); router.use(bodyParser.json()); router.use(cookieParser()); router.use(librequire("requester").middleware);
const testpostRouteDefs = {
body: {
passedTxt: ApiDef.Field.StringRequired,
secretPwd: ApiDef.Field.StringRequired,
additionalVal: ApiDef.Field.Int
}
};
router.post("/test-post", new ApiEndpoint(testpostRouteDefs, function (req, res) {
const {passedTxt, additionalVal, secretPwd} = req.body;
res.json({status: "ok", passedArgs: {...req.body}});
}));
router.get("/test", new ApiEndpoint({}, function (req, res) {
res.json({msg: "test endpoint works!"});
}));
router.subRouter(require("./products/products.router"));
router.subRouter(require("./access/access.router"));
router.use(require("./middleware/errorHandler"));`