'use strict'; const koa = require('koa'); const compress = require('koa-compress'); const logger = require('koa-logger'); const koaBody = require('koa-body'); const { userAgent } = require('koa-useragent'); const serve = require('koa-static'); const cors = require('koa2-cors'); const path = require('path'); const Redis = require('ioredis'); const Router = require('koa-router'); const app = module.exports = new koa(); const server = require('http').createServer(app.callback()); app.use(logger()); app.use(compress()); app.use(koaBody()); app.use(userAgent); app.use(cors({ origin: "*" })); app.use(serve(path.join(__dirname, './public'))); let db = new Redis(6379, '39.103.163.220', { family: 4, db: 0, password: 'ehason' }) const Token = require("../auth/index")(db, 60 * 30); const router = new Router(); router.get('/gen', async (ctx, next) => { let tk = await Token.genToken({ uid: '1111', appid: '222' }) let a = await Token.checkToken(tk) let d = await Token.getTokenData(tk) ctx.request.body.token = tk; Token.checkTokenKoaRequest(ctx, next) ctx.body = { token: a } }); router.get('/check', async (ctx, next) => { Token.checkTokenKoaRequest(ctx, 'key', next) }); app.use(router.routes()); //作用:启动路由 app.use(router.allowedMethods()); //作用: 当请求出错时的处理逻辑 // Start http server let port = process.env.PORT ? process.env.PORT : 9000; server.listen(port); console.log('Http api serve at port: ' + port);