注册与登陆

用户注册

修改 index.ts

import * as Koa from 'koa';
import * as OtherParser from 'koa-better-body';
import * as bodyParser from 'koa-bodyparser';
// import * as Router from 'koa-better-router';
import * as Convert from 'koa-convert';
import { api, router } from './router';

const app = new Koa();

app
.use(Convert(bodyParser()))
.use(Convert(OtherParser()))
.use(router.middleware())
.use(api.middleware())
.listen(3000, () => {
    console.log("Server Stared on http://localhost:3000");
    api.getRoutes().forEach((route) => {
         console.log(`${route.method} http://localhost:3000${route.path}`)
    })
});

同样在src下面增加 router.ts 用来控制路由

在 src 创建controller文件夹,再新建 user.ts

假如你想测试一下有没有成功,可以用postman测试一下,必须要以formdata的方式传递参数。

增加登陆逻辑

controller/user.ts

这里我们提取了一些公共方法出来用来减少代码量,返回的时候不要把密码返回给用户。

JSON.parse(JSON.stringify(user)) 用于深复制,起初我只是简单的 JSON.stringify ,我想当然的认为已经是一个新对象了,结果password就是删除不掉。

在 router.ts 增加路由

Last updated