Modular-Rest
a nodejs module based on KOAJS for developing Rest-APIs in a modular solution.
- modular-rest is only for developing rest api, and is not based on MVC.
- each route would be a module in this system. you can define your base routes on difrent folders then the Modular-Rest would combine all routes to main app object.
this module has one method:
createRest
: is the main functionality of the module.
Install
Install using npm:
npm i modular-rest --save
createRest
use modular-rest to work with modular-rest you need an app.js
and a routers
folder. then configuring app.js
and put your each router as a subfolder into the routers
folder.
app.js
configuring simple configuration of app.js
with koa-router
module.
const modularRest = ;let koaBody = ; let option = root: onBeforInit: BeforInit // befor anything onInit: Init // after collecting routers onAfterInit: AfterInit // affter launch server port: 80 // if it would be true, app doesn't listen to port, // and a raw app object with all routers will be returned. // this option is for virtual host middlewares dontlisten: false // collecting other services from subfolders otherSrvice: filename: name: 'fn' extension:'.js' rootDirectory: option: // if this option woulde be true, the property of each service will be attached to rootObject // the `name` property will be rejected and only the main property of each service would be recognize. // it would be useful when you want to collect all mongoose models in one root object. combineWithRoot: false // convert the rootObject to an array // the `name` property will be rejected and only the main property of each service would be recognize. convertToArray: false ; { // do something} { // use a body parser app; // use otherSrvice // all your other services will injected to `otherSrvice` object. // eahc service would be accessible by its filename globalservices = otherSrvice'fn';} { // do something} modularRest;
configuring a route
- go to
routers
folder and create a subfolder calledsearch
folder. - go to
search
folder and create aroute.js
file, then put these lines into it.
let Router = ;let name = 'search'; let search = ;search; search moduleexportsname = name;moduleexportsmain = search;
Requesting
your search web service is:
http://localhost:80/search
thank you for using Modular-Rest :)