/koa-sample

koajs assembly example

Primary LanguageJavaScript

Koa sample

Demonstrate how a koa application can be assemble.

App

This example show how to set up an application that register a shared api middleware and load all the routes located in controllerPaths

var koa = require('koa');
var api = require('./lib/middleware/api-middleware');
var app = module.exports = koa();

app.use(api({ // register a global middleware
  baseUrl: "http://localhost:4000"
}));

router({
  controllerPaths: __dirname + '/controller',
});

Controller

Controller is responsible for processing http requests and returning http responses. A basic implementation looks like this :

var Joi = require('joi');

module.exports = {
  'main': {
    path: '/',
    methods: ['get'],
    validate: {
      query: {
        foo: Joi.string().required()
      }
    },
    handler: function *() {
      // this.api is available globally because of app.use()
    }
  }
};

Logging

Logging relays on console.log, following the twelve factor principle.

this.logger({
  type: 'foo.bar',
  preview: '{bar}',
  fields: {
    'bar': 'foo'
  }
});

In production, logs are serialized as json for machine processing (logstash, loggly etc...).

In development, the preview attributes tells to the logger how to output extra parameters :

Request correlation

Because the logger middleware depends on koa-request-id, the request id is automatically added to all log entries in the id property.

Metrics

The following metrics are available out of the box :

Metric collection Metric key
api error
api response.{status}
server http-request.{method}
server http-response.{status}
app error.validation.{route}.{location}