From 1c0f15491eb393e7ffe79bcf701446b2a0378870 Mon Sep 17 00:00:00 2001 From: lik Date: Mon, 29 Jun 2020 13:26:37 +0800 Subject: [PATCH] Add test --- test/package.json | 29 +++++++++++++++++++++++ test/public/index.html | 54 ++++++++++++++++++++++++++++++++++++++++++ test/test.js | 44 ++++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 test/package.json create mode 100644 test/public/index.html create mode 100644 test/test.js diff --git a/test/package.json b/test/package.json new file mode 100644 index 0000000..b846f93 --- /dev/null +++ b/test/package.json @@ -0,0 +1,29 @@ +{ + "name": "test", + "version": "1.0.0", + "description": "", + "main": "test.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "ioredis": "^4.17.3", + "bluebird": "^3.7.2", + "jsonwebtoken": "^8.5.1", + "koa": "^2.12.0", + "koa-body": "^4.1.2", + "koa-compress": "^4.0.1", + "koa-logger": "^3.2.1", + "koa-router": "^8.0.8", + "koa-static": "^5.0.0", + "koa-useragent": "^4.0.0", + "koa2-cors": "^2.0.6", + "moment": "^2.25.3", + "mongoose": "^5.9.15", + "request": "^2.88.2", + "request-promise": "^4.2.5", + "socket.io": "^2.3.0" + } +} diff --git a/test/public/index.html b/test/public/index.html new file mode 100644 index 0000000..17d8fc7 --- /dev/null +++ b/test/public/index.html @@ -0,0 +1,54 @@ + + + + + + + + + Bootstrap 101 Template + + + + + + + + + + +

+ + + + + + + + + + + \ No newline at end of file diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..71d65e0 --- /dev/null +++ b/test/test.js @@ -0,0 +1,44 @@ +'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); +