const Koa = require('koa') const app = new Koa() const views = require('koa-views') const json = require('koa-json') const onerror = require('koa-onerror') const bodyparser = require('koa-bodyparser') const logger = require('koa-logger') const cors = require('koa-cors'); const koaBody = require('koa-body'); const route = require('koa-router'); // app.use(route.post('/profile', uploads.single('avatar'))); const index = require('./routes/index') const users = require('./routes/users') const meeting = require('./routes/meeting') const studentMeeting = require('./routes/studentMeeting') const upload = require('./routes/upload') const email = require('./routes/email') const RecordInfo = require('./routes/RecordInfo') const filterUrl = require(__dirname+'/util/filterUrl') var tokenUtil = require('./util/tokenUtil'); const _ = require('lodash'); var status = require('./util/resTemplate') // error handler onerror(app) app.use(koaBody({ multipart: true })); // middlewares app.use(cors()); app.use(json()) app.use(logger()) app.use(require('koa-static')(__dirname + '/public')) app.use(views(__dirname + '/views', { extension: 'html' })) // logger app.use(async (ctx, next) => { try{ const start = new Date(); const url = ctx.request.url.split('?'); if(true){ await next(); }else if(filterUrl.indexOf(url[0]) != -1){ await next(); }else if(!ctx.header.token){ status.catchError(ctx,400,'请登录'); }else{ let isToken = await tokenUtil.prverifySession(ctx.header.token); ctx.request.userID = isToken.id; ctx.request.userName = isToken.userName; ctx.request.userRole = isToken.userRole; await next(); } const ms = new Date() - start; console.log(`${ctx.method} ${ctx.url} - ${ms}ms`) }catch (e){ status.catchError(ctx,400,e.message); } }) // routes app.use(index.routes(), index.allowedMethods()) app.use(users.routes(), users.allowedMethods()) app.use(meeting.routes(), meeting.allowedMethods()) app.use(studentMeeting.routes(), studentMeeting.allowedMethods()) app.use(upload.routes(), upload.allowedMethods()) app.use(email.routes(), email.allowedMethods()) app.use(RecordInfo.routes(), RecordInfo.allowedMethods()) module.exports = app