koa /account/user/:id 这样的接口怎么写
时间: 2023-10-01 19:03:37 浏览: 158
userauth:koa用户身份验证中间件
这个接口可以使用koa-router来实现,可以按照以下步骤来完成:
1. 引入koa-router
```javascript
const Router = require('koa-router');
const router = new Router();
```
2. 定义路由
```javascript
router.get('/account/user/:id', async (ctx) => {
const userId = ctx.params.id;
// 根据userId查询用户信息
// ...
ctx.body = { user: userInfo };
});
```
3. 注册路由
```javascript
app.use(router.routes()).use(router.allowedMethods());
```
完整代码示例:
```javascript
const Koa = require('koa');
const Router = require('koa-router');
const app = new Koa();
const router = new Router();
router.get('/account/user/:id', async (ctx) => {
const userId = ctx.params.id;
// 根据userId查询用户信息
// ...
ctx.body = { user: userInfo };
});
app.use(router.routes()).use(router.allowedMethods());
app.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});
```
阅读全文