koa-context-loader
v1.0.1
Published
load anything to koa context
Downloads
11
Maintainers
Readme
koa-context-loader
load file module into app.context
Install
npm install koa-context-loader
yarn add koa-context-loader
Usage
// ./service/user.js
module.exports = {
getUserId() {
return 123;
}
}
// ./app.js
const Koa = require('koa');
const app = new Koa();
const loader = require('koa-context-loader');
const path = require('path');
const options = {
service: path.resolve(__dirname, './service'),
alias: 'path'
}
loader(app.context, options)
app.use((ctx, next) => {
// ctx[alias][filename][export_method]
const id = ctx.service.user.getUserId(); // id:123
});
API
options {alias: keyof options, path: string}
options = {
alias: path
}
path must be absolute