Created
March 1, 2016 21:32
-
-
Save boushley/a617d192618ad205eef8 to your computer and use it in GitHub Desktop.
Example KOA Server to serve static files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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