Skip to content

backkit/koa

Repository files navigation

Koa service for backkit

Interactive + Autoconf Install

Use this to generate custom configuration post install

npm install --foreground-scripts --progress false \
            @backkit/kernel \
            @backkit/koa \
            @backkit/config \
            @backkit/winston --save

Non-Interactive Install with Default Autoconf Install

Use this to generate fast default configuration without prompt post install

npm install @backkit/kernel \
            @backkit/koa \
            @backkit/config \
            @backkit/winston --save

Non-Interactive Non-Autoconf Install

Use this in production and CI

NO_AUTOCONF=y \
npm install @backkit/kernel \
            @backkit/koa \
            @backkit/config \
            @backkit/winston --save

For all of the above, use clean-install to reinstall existing packages

Simple controller

res/koa/hello.js

module.exports = ({koa}) => {
  // returns new koa router instance
  const router = koa.router;

  router
  .get('/', (ctx, next) => {
    ctx.body = "test"
  })
  .get('/hello', (ctx, next) => {
    ctx.body = `Hello ${ctx.query.name}`
  })

  koa.useRouter(router);
};

Class based controller (with binding)

res/koa/hello.js

class HelloController {

  constructor({koa}) {
    this.name = 'Koa';
    koa.useRouter(koa.router
      .get('/', this.index.bind(this))
      .get('/hello', this.hello.bind(this)));
  }

  async index(ctx, next) {
    ctx.body = "index"
  }

  async hello(ctx, next) {
    // note the this.name default value
    ctx.body = `hello ${ctx.query.name||this.name}`
  }
}

module.exports = HelloController;

Use Passport.js

First install koa-passport

npm install koa-passport --save

Initialize it res/koa/passport.js

const passport = require('koa-passport');

module.exports = ({koa}) => {
  ...
  // initialize passport
  koa.useMiddleware(passport.initialize())
  koa.useMiddleware(passport.session())
  ...
}

then use it in your controller: res/koa/hello.js

class HelloController {

  constructor({koa}) {
    koa.useRouter(koa.router
      .get('/', this.index.bind(this)));
  }

  async index(ctx, next) {
    if (ctx.isAuthenticated()) {
      ctx.body = "I am logged in";
    } else {
      ctx.body = "I am logged out";
    }
  }
}

module.exports = HelloController;

Links

https://nodei.co/npm/@backkit/koa?downloads=true&downloadRank=true&stars=true

GitHub CI

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published