koa-helper

1.0.8 • Public • Published

A helper function to build a server easily using Koa & EJS

Usage

const koahelper = require('koa-helper');
const path = require('path');

(async () => {
    const db = ..../* Open database */
    const app = await koahelper(
        process.env.port || 3000,
        path.join(__dirname, 'src', 'controller'),
        path.join(__dirname, 'src', 'view'),
        [ path.join(__dirname, 'web'), path.join(__dirname, 'node_modules', 'bootstrap', 'dist') ],
        {
            dev: (process.env.dev == 'true'),
            started: () => app.context.db = db,
            stopped: () => app.context.db = null || db.close()
        }
    );
})();

Controller looks like

async function all() {
    this.body = await this.render('users/all', { code: 'hello' });
}

module.exports = {
    use: app => {
        const R = require('koa-route');
        app.use(R.get('/users/all', all));
    }
};

File structure looks like

app
|-- src
    |-- controller
        |-- users.js
    |-- view
        |-- users
            |-- all.ejs
|-- web
    |-- css
    |-- js
|-- node_modules
    |-- bootstrap
        |-- dist
            |-- css
            |-- js
    |-- ...
|-- app.js

Have fun !

Readme

Keywords

Package Sidebar

Install

npm i koa-helper

Weekly Downloads

0

Version

1.0.8

License

ISC

Unpacked Size

4.2 kB

Total Files

3

Last publish

Collaborators

  • xicabin