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.
Note: active "javascript.implicitProjectConfig.checkJs": true in your vscode settings.
Install
Install using npm:
npm i modular-rest --save
use modular-rest createRest
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.
configuring app.js
simple configuration of app.js
with koa-router
module.
const modularRest = require('modular-rest');
let koaBody = require('koa-body');
let option = {
root: require('path').join(__dirname, 'routers'),
onBeforInit: BeforInit,
onInit: Init,
onAfterInit: AfterInit,
port: 80,
dontlisten: false,
otherSrvice: [
{
filename: {name: 'fn', extension:'.js'},
rootDirectory: require('path').join(__dirname, 'routers'),
option: {
combineWithRoot: false,
convertToArray: false,
}
}
],
};
function BeforInit(app)
{
app.use(koaBody());
}
function Init(app, otherSrvice)
{
global.services = otherSrvice['fn'];
}
function AfterInit(app, otherSrvice) {
}
modularRest.createRest(option).then(app => {
});
configuring a route
- go to
routers
folder and create a subfolder called search
folder. - go to
search
folder and create a route.js
file, then put these lines into it.
let Router = require('koa-router');
let name = 'search';
let search = new Router();
search.get('/', (ctx) => {
ctx.body = 'send me a post request';
});
search.post('/', (ctx) => {
ctx.body = `Request Body: ${JSON.stringify(ctx.request.body)}`;
})
module.exports.name = name;
module.exports.main = search;
Requesting
your search web service is:
http://localhost:80/search
thank you for using Modular-Rest :)