mirror of
https://github.com/koajs/examples.git
synced 2025-12-08 19:24:58 +00:00
33 lines
570 B
JavaScript
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);
|