KOA-CORE-SERVER
The koa server wrapper. That's all what you need to start build koa based server.
SETUP
- npm install --save koa-core-server
HOW TO USE
Project structure should looks like this.
const Server = require('koa-core-server');
const path = require('path');
const routePath = path.join(__dirname, 'routers');
const controllersPath = path.join(__dirname, 'controllers');
const dataAccessPath = path.join(__dirname, 'dataAccess');
const server = new Server({
routePath,
controllersPath,
dataAccessPath,
clustering: {bool},
loadersStatus: {bool},
});
server.use((ctx, next) => {
console.log('hello from middleware');
next();
});
server.start();
const Router = require('koa-router');
module.exports = class HelloRouter extends Router {
constructor() {
super();
this.name = 'Hello router';
this.rootPath = null;
this.helloController = this.injector.get('helloCustomName');
}
load() {
this.get('/', this.helloController.getHello.bind(this.helloController));
this.get('/by', this.helloController.sayBy.bind(this.helloController));
return this;
}
};
module.exports = class HelloController {
constructor() {
this.name = 'helloCustomName';
this.helloDataAccess = this.injector.get('helloDataAccess');
}
async getHello(ctx, next) {
ctx.response.body = await this.helloDataAccess.getHello();
}
async sayBy(ctx, next) {
ctx.response.body = await this.helloDataAccess.getGoodBy();
}
}
module.exports = class HelloDataAccess {
getHello() {
return Promise.resolve('hello');
}
getGoodBy() {
return Promise.resolve('good by');
}
}
For more detailed example look at server example
TODO
- add load packages from yarn
- add only read preferences to injectors
- add tests
- add auto publish via some CI by master hook