Created
November 15, 2015 06:40
-
-
Save ruandao/a21ff353c9e8aad79d8e to your computer and use it in GitHub Desktop.
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
"use strict"; | |
var koa = require('koa'); | |
var app = koa(); | |
var bodyParser = require('koa-bodyparser'); | |
var fs = require('fs'); | |
app.use(bodyParser()); | |
app.use(function*(next) { | |
if (this.path !== '/') { | |
return yield next; | |
}; | |
var name = this.request.body.name; | |
this.body = name.toUpperCase(); | |
}); | |
app.use(function*(next) { | |
if (this.path !== '/json') { | |
yield next; | |
}; | |
this.body = { foo: 'bar'}; | |
}); | |
app.use(function*(next) { | |
if (this.path !== '/stream') { | |
yield next; | |
}; | |
this.body = fs.createReadStream(process.argv[3]); | |
}) | |
app.use(handler404); | |
app.use(hander500); | |
function* handler404 (next) { | |
if (this.path !== '/404') { | |
return yield next; | |
}; | |
this.body = 'page not found'; | |
} | |
function* hander500 (next) { | |
if (this.path !== '/500') { | |
return yield next; | |
}; | |
this.body = 'internal server error'; | |
} | |
var port = process.argv[2]; | |
app.listen(port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment