Koa入门第三课:options 方法及 RESTful API 最佳实践,和设置响应内容

options

介绍

用于探查目标接口,允许那些请求方式。

Koa-Router 开启 options

只需1行代码;

app.use(usersRouter.allowedMethods());

RESTful API 最佳实践

获取 get

返回详情

提交 post

返回新建的数据

更新 put

修改后的详情

删除 delete

返回 204 (Not Content)

设置响应内容

usersRouter.get('/', (ctx) => {
    ctx.throw(404)
    ctx.status = 200; // 设置状态码
    ctx.body = [ // 设置请求体
        {
            name: '刘利豪',
            age: 22
        }
    ];
    ctx.set('Allow', 'GET, POST'); // 设置请求头
});

posted @ 2019-12-13 01:20  利豪  阅读(510)  评论(0)    收藏  举报