-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
29 lines (28 loc) · 1.06 KB
/
Copy pathapi.js
File metadata and controls
29 lines (28 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const Router = require('koa-router');
let router = new Router();
module.exports = {
restify: () => {
return async (ctx, next) => {
// 是否是REST API前缀判断
if (ctx.request.path.startsWith('/api/')) {
ctx.json = (data = {}, code = 0, msg = 'ok') => {
ctx.response.type = 'application/json; charset=utf-8';
ctx.response.body = { code, msg, data };
};
ctx.restError = (msg = '', code = -1, status = 400) => {
code = code || 'internal:unknown_error';
ctx.response.status = status;
ctx.response.type = 'application/json; charset=utf-8';
ctx.response.body = { code, msg };
};
}
await next();
};
},
routes: () => {
require(`${__dirname}/../api/index.js`).map((subrouters, i) => {
router.use(subrouters.routes()).use(subrouters.allowedMethods());
});
return router.routes();
},
};