dongyu 50bcadf73e migrate to koa@2 (#108)
* support async/await

* change nodejs version 4 or 6 to 5.6

* eslint

* remove jade
2017-05-30 20:44:57 +08:00

33 lines
570 B
JavaScript

const Koa = require('koa');
const auth = require('koa-basic-auth');
const app = module.exports = new Koa();
// custom 401 handling
app.use(async function(ctx, next) {
try {
await next();
} catch (err) {
if (err.status === 401) {
ctx.status = 401;
ctx.set('WWW-Authenticate', 'Basic');
ctx.body = 'cant haz that';
} else {
throw err;
}
}
});
// require auth
app.use(auth({ name: 'tj', pass: 'tobi' }));
// secret response
app.use(async function(ctx) {
ctx.body = 'secret';
});
if (!module.parent) app.listen(3000);