-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
33 lines (27 loc) · 837 Bytes
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const logger = require('koa-logger');
const Koa = require('koa');
const app = module.exports = new Koa();
// passing any middleware to this middleware
// will make it conditional, and will not be used
// when an asset is requested, illustrating how
// middleware may "wrap" other middleware.
function ignoreAssets (mw) {
return async function (ctx, next) {
if (/(\.js|\.css|\.ico)$/.test(ctx.path)) {
await next();
} else {
// must .call() to explicitly set the receiver
// so that "this" remains the koa Context
await mw.call(this, ctx, next);
}
};
}
// TRY:
// $ curl http://localhost:3000/
// $ curl http://localhost:3000/style.css
// $ curl http://localhost:3000/some.html
app.use(ignoreAssets(logger()));
app.use(async function (ctx) {
ctx.body = 'Hello World';
});
app.listen(3000);