Add test
This commit is contained in:
		
							parent
							
								
									c55a29dfd2
								
							
						
					
					
						commit
						1c0f15491e
					
				
							
								
								
									
										29
									
								
								test/package.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								test/package.json
									
									
									
									
									
										Normal file
									
								
							@ -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"
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										54
									
								
								test/public/index.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								test/public/index.html
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,54 @@
 | 
			
		||||
<!DOCTYPE html>
 | 
			
		||||
<html lang="zh-CN">
 | 
			
		||||
 | 
			
		||||
<head>
 | 
			
		||||
    <meta charset="utf-8">
 | 
			
		||||
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
 | 
			
		||||
    <meta name="viewport" content="width=device-width, initial-scale=1">
 | 
			
		||||
    <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
 | 
			
		||||
    <title>Bootstrap 101 Template</title>
 | 
			
		||||
 | 
			
		||||
    <!-- Bootstrap -->
 | 
			
		||||
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
 | 
			
		||||
 | 
			
		||||
    <!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询(media queries)功能 -->
 | 
			
		||||
    <!-- 警告:通过 file:// 协议(就是直接将 html 页面拖拽到浏览器中)访问页面时 Respond.js 不起作用 -->
 | 
			
		||||
    <!--[if lt IE 9]>
 | 
			
		||||
    <script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
 | 
			
		||||
    <script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
 | 
			
		||||
    <![endif]-->
 | 
			
		||||
</head>
 | 
			
		||||
 | 
			
		||||
<body>
 | 
			
		||||
    <h1></h1>
 | 
			
		||||
 | 
			
		||||
    <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
 | 
			
		||||
    <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
 | 
			
		||||
    <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
 | 
			
		||||
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
 | 
			
		||||
 | 
			
		||||
    <script>
 | 
			
		||||
        $(document).ready(function () {
 | 
			
		||||
            $.ajax({
 | 
			
		||||
                url: 'gen',
 | 
			
		||||
                type: 'post',
 | 
			
		||||
                dateType: 'json',
 | 
			
		||||
                data: JSON.stringify({ o: 'aaa' }),
 | 
			
		||||
                success: function (data) {
 | 
			
		||||
                    $.ajax({
 | 
			
		||||
                        url: 'check',
 | 
			
		||||
                        type: 'post',
 | 
			
		||||
                        headers: { 'aaa': data.token, 'content-Type': "application/json" },
 | 
			
		||||
                        data: JSON.stringify({ aaa: data.token }),
 | 
			
		||||
                        success: function (data) { console.log("sucess"); },
 | 
			
		||||
                        error: function (data) { console.log("error"); }
 | 
			
		||||
                    });
 | 
			
		||||
                },
 | 
			
		||||
                error: function (data) { console.log("error"); }
 | 
			
		||||
            });
 | 
			
		||||
        });
 | 
			
		||||
    </script>
 | 
			
		||||
 | 
			
		||||
</body>
 | 
			
		||||
 | 
			
		||||
</html>
 | 
			
		||||
							
								
								
									
										44
									
								
								test/test.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								test/test.js
									
									
									
									
									
										Normal file
									
								
							@ -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);
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user