@daniellehuisman/koa-base
TypeScript icon, indicating that this package has built-in type declarations

1.3.1 • Public • Published

koa-base

Koa server with basic middleware.

Middleware

Installation

yarn add @danielhuisman/koa-base

Usage

import {createServer, logger, startServer} from '@danielhuisman/koa-base';
import Router from 'koa-router';
import path from 'path';

const config = {
    port: 5000,

    session: {
        secret: 'sessionSecret'
    },

    static: {
        serve: process.env.STATIC_SERVE !== 'false',
        path: path.join(__dirname, '..', 'static')
    }
};

(async () => {
    logger.info('Starting application...');

    // Initialize server
    const {server, app} = createServer(config);

    // Initialize router
    const router = new Router();

    // Index route
    router.get('/', async (ctx) => {
        return ctx.success(
            {
                message: 'Hello World!'
            },
            200
        );
    });

    // Add router
    app.use(router.routes());
    app.use(router.allowedMethods());

    // Handle unknown routes
    app.use(async (ctx, next) => {
        ctx.error(404, 'Not found');
        await next();
    });

    // Start server
    await startServer(config, server);

    logger.info('Started application.');
})();

Package Sidebar

Install

npm i @daniellehuisman/koa-base

Weekly Downloads

4

Version

1.3.1

License

MIT

Unpacked Size

17.4 kB

Total Files

20

Last publish

Collaborators

  • daniellehuisman