koa

之间看示例,和express不同,koa的router需要自己引入。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const Redis = require('ioredis');
const Koa = require('koa');
const Router = require('koa-router');
const router = new Router();
const app = new Koa();
const redis = new Redis(`redis://127.0.0.1:6379/0`);
router.get('/', async (ctx, next) => {
await next();
await redis.incr('pv');
const current = await redis.get('pv');
ctx.body = `current pv: ${current}`;
});
app
.use(router.routes())
.use(router.allowedMethods());
app.listen(3000);

扫一扫,分享到微信

请我喝杯咖啡吧~