Skip to content

Instantly share code, notes, and snippets.

@boushley
Created March 1, 2016 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boushley/a617d192618ad205eef8 to your computer and use it in GitHub Desktop.
Save boushley/a617d192618ad205eef8 to your computer and use it in GitHub Desktop.
Example KOA Server to serve static files
var fs = require('fs');
var koa = require('koa');
var r = require('koa-route');
var bodyParser = require('koa-bodyparser');
var app = koa();
app.use(function *(next){
var start = new Date;
yield next;
var ms = new Date - start;
console.log('%s %s - %s', this.method, this.url, ms);
});
app.use(bodyParser());
app.use(r.get('/bar.json', function *() {
this.type = 'application/json;charset=utf-8';
this.body = fs.createReadStream('./responses/bar.json');
}));
app.use(r.get('/foo.xml', function *() {
this.type = 'text/xml;charset=utf-8';
this.body = fs.createReadStream('./responses/foo.xml');
}));
app.listen(3087);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment