1.0.1 • Published 6 years ago

koa-context-loader v1.0.1

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

koa-context-loader

NPM version build status code coverage license

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