Reusable middlewares, utils, and helper functions for Express.js
Why?
- Reuse implies efficiency
- Save your time from copying Express.js middlewares
- Encapsulate all related dependencies so that to create a more cleaner repo
Usages
Connect to the server
Focus on your routes and business logic:
; const listener = ; listener;
How can it save your time?
{ // Prepend neccessary middlewares before matching any routes ; // Handle external middlewares ; // Append middlewares that catches remaining requests ; // Return a promise so that external service can execute post action after the server started return ;};
Express Middlewares
You can use different middleware in a flexible way:
;; const server = ;const middleware errorHandler listener = middlewares; ; // ... the rest of the logic ;
The middleware helper functions have the most commonly needed middlewares:
server; server; server; server; server;;
CURD Creators
Encapsulate most reusable CURD actions into separated modules
;const actionCreator modelCreator routeCreator = curd;
actionCreator.js
Example of { Model; } // ... the rest of actions;
routeCreator.js
Example of { router ; return router;};
modelCreator.js
Example of You can define any additional schema to that mongoose scheme:
const DEFAULT_SCHEMA = created_at: type: 'Date' default: Datenow required: false updated_at: type: 'Date' default: Datenow required: false ; { if throw 'schema'; return Object;};
By doing this you can use custom method of mongoose schema before initializing it as a mongoose model.
const userModel = mongoose;
Utils
;const connectDb seedData dropDb = utils;
Connecting Database
A Promise handler will be returned by a given MongoDB url:
;
Seeding data to database
It will clean up your database and pass each records to Promise.all for resolving.
;;
Drop Connection
Normally used after running each test cases:
;