'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, '192.168.0.2', { family: 4, db: 0, password: 'sleton' }) const Token = require("../auth/index")(db); const router = new Router(); router.post('/gen', async (ctx, next) => { let a = await Token.genToken({ name: 'like' }, 'key', 100) ctx.body = { token: a } }); router.post('/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);