0.5.6 • Published 9 years ago
kaio v0.5.6
kaio
A minimalist middleware to get started faster with Koa.
Installation
npm install kaio --save
Example
Requirements
mkdir kaio-app
cd kaio-app
npm install kaio koa-router
touch app.js
// app.js
var kaio = require('kaio');
var router = require('koa-router');
kaio()
.setRoot(__dirname)
.setHost('127.0.0.1')
.setPort(3000)
.setUri('/api')
.bind('/', BaseController)
.bind('/books', BookController)
.listen();
function *BaseController(next) {
yield next;
if (this.res.statusCode === 200)
return;
this.body = 'Hello world!';
}
function BookController() {
var dataset = [
{ title: "The Fellowship of the Ring", author: "J. R. R. Tolkien", publication: "1954-07-29" },
{ title: "The Two Towers", author: "J. R. R. Tolkien", publication: "1954-11-11" },
{ title: "The Return of the King", author: "J. R. R. Tolkien", publication: "1955-10-20" }
];
var list = function *(next) {
var res = dataset;
yield next;
this.body = res;
};
var show = function *(next) {
var title = decodeURI(this.params.title);
var res = dataset.filter(function(x) {
return title === x.title;
}).shift();
yield next;
this.body = res;
};
return router()
.get('/', list)
.get('/:title', show)
.middleware();
}
Run the application
$ KO_PORT=1333 DEBUG=* node --harmony app.js
Test it
# 3000 is the default port but it is overriden by KO_PORT (1333)
$ curl http://localhost:1333/api/
Not found
$ curl http://localhost:1333/api/
Hello world!
$ curl http://localhost:1333/api/books
[{"title":"The Fellowship of the Ring","author":"J. R. R. Tolkien","publication":"1954-07-29"},{"title":"The Two Towers","author":"J. R. R. Tolkien","publication":"1954-11-11"},{"title":"The Return of the King","author":"J. R. R. Tolkien","publication":"1955-10-20"}]
$ curl http://localhost:1333/api/books/The%2520Two%2520Towers
{"title":"The Two Towers","author":"J. R. R. Tolkien","publication":"1954-11-11"}
API
Tests
npm test
Release History
- 0.5.0 Use confectioner module, add tests and upgrade public API
- 0.3.0 Replace custom resolver by dotresolver
- 0.1.0 Initial release