mirror of
https://github.com/koajs/examples.git
synced 2026-01-25 14:48:15 +00:00
31 lines
498 B
JavaScript
31 lines
498 B
JavaScript
var koa = require('koa');
|
|
var auth = require('koa-basic-auth');
|
|
var app = module.exports = koa();
|
|
|
|
// custom 401 handling
|
|
|
|
app.use(function* (next){
|
|
try {
|
|
yield* next;
|
|
} catch (err) {
|
|
if (401 == err.status) {
|
|
this.status = 401;
|
|
this.body = 'cant haz that';
|
|
} else {
|
|
throw err;
|
|
}
|
|
}
|
|
});
|
|
|
|
// require auth
|
|
|
|
app.use(auth({ name: 'tj', pass: 'tobi' }));
|
|
|
|
// secret response
|
|
|
|
app.use(function* (){
|
|
this.body = 'secret';
|
|
});
|
|
|
|
if (!module.parent) app.listen(3000);
|