express-on
v3.0.3
Published
Mongoose + Express + Controllers + Routes + Formatters + Swagger + ReDoc + CLI
Downloads
45
Readme
Express-on
is a minimal MVC framework to easily turn mongodb collections into fully fledged RESTful services supporting all http verbs.
Express is the http framwork used to handle the REST service request/response using Mongoose as the ORM layer on top of mongodb and binds the http verbs to the Query operations.
HTTP Verbs
- GET binds to Query.find
- DEL binds to Query.deleteMany
- PUT binds to Query.replaceOne
- POST binds to Model.insertMany
- PATCH binds to Query.updateMany
MVC Pattern
models
folder contains the mongoose schemas/models that defines the object modelling for the document resides in an underlying mongodb collection, it is also used for document level validation and any other mongoose specific hooks as per the Document api, more on mongoose schemasroutes
folders containes the http routes binding for different verbs to the underlying controllers method passing the http request query params along the request. more on Express routercontrollers
folder containes the business logic needed for each operation pre submitting the operation to mongodb by simply overrding any of base Controller implementation
Controller Class
Controller
class is a base class with a constructor that takes the mongooseModel
as the only parameter, and implements the following methods that can be overriden to implement any custom logic, checks, etc
- find
- insertMany
- deleteMany
- updateMany
- replaceMany
- findById
- findByIdAndDelete
- findByIdAndUpdate
- findByIdAndReplace
Controller
class implementation for the methods also handles the following query parameters that might present in the request url and invokes the relevant mongoose Query method
- find=
{Object}
specifies the mongodb selector. If not specified, returns all documents. - limit=
Number
specifies the maximum number of documents the query will return, defaults to 10, and return all when explicitly set to 0. - skip=
Number
specifies the number of documents to skip. useful for pagination when combined with limit. - count=
Boolean
set totrue
to count documents matching the mongodb selector if specified, otherwise returns the total count of documents - distinct=
String
specifies a single path to get the distict values of. - sort=
String
sets the sort order, it must be a space delimited list of path names. The sort order of each path is ascending unless the path name is prefixed with-
which will be treated as descending. - select=
String
specifies which document fields to include or exclude, prefixing a path with-
will flag that path as excluded. When a path does not have the - prefix, it is included.
Express Router
Express routers is used to bind the http requests to the underlying controller that implements the requested operation, example
Getting Started
Install with peer dependencies:
npm install --save express
npm install --save mongoose
npm install --save express-on
Edit package.json to add start script:
"main": "node_modules/express-on",
"scripts": {
"start": "node_modules/.bin/express-on"
}
Create models\currencies.js
with a basic Mongoose model:
import mongoose from 'mongoose'
let schema = new mongoose.Schema({
_id: String,
name: String,
country: String
});
const Model = mongoose.model('currency', schema, 'currencies');
export default Model;
Create controllers\currencies.js
with a basic controller
import { Controller } from 'express-on/controller';
import Model from '../models/currencies.js';
export class CurrenciesController extends Controller {
constructor() {
super(Model)
}
}
export default CurrenciesController;
Create routers\index.js
to bind http routes to controller methods
import Controller from '../controllers/currencies.js';
import { Router } from 'express';
const controller = new Controller();
const router = Router();
router.delete('/currencies/:id', controller.findByIdAndDelete.bind(controller));
router.patch('/currencies/:id', controller.findByIdAndUpdate.bind(controller));
router.put('/currencies/:id', controller.findByIdAndReplace.bind(controller));
router.get('/currencies/:id', controller.findById.bind(controller));
router.delete('/currencies/', controller.deleteMany.bind(controller));
router.patch('/currencies/', controller.updateMany.bind(controller));
router.post('/currencies/', controller.insertMany.bind(controller));
router.put('/currencies/', controller.replaceMany.bind(controller));
router.get('/currencies/', controller.find.bind(controller));
export default router;
Configure mongodb connection string:
set MONGO_URI=mongodb://localhost/test
Configure http port to listen on:
set PORT=80
Start via
node .