4. 处理HTTP请求
1. 简单处理HTTP
中间件处理请求
// router.js
async function handleReq(ctx, next) {
// 通过ctx.req.url可以请求的url
ctx.body = ctx.req.url.replace(/(\/)/gi, ' ');
await next();
}
module.exports = {
handleReq };
详细request内包含的属性可查看: Koa2 context.request
2. Get请求
- 处理Get请求
// queryRequest.js
const Koa = require('koa');
const app = new Koa();
const Router = require('koa-router');
const apiRouter = new Router();
apiRouter.get('/getName', async ctx => {