-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
63 lines (55 loc) · 1.68 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const express = require("express");
const path = require("path");
const fs = require("fs");
const captchapng = require('captchapng');
const jwt = require("jsonwebtoken");
const app = express();
const privateKey = "laobao101";
app.use(express.static(__dirname + "/public"));
app.get("/checkCode", (req, res) => {
const { query } = req;
const { token, code } = query;
try {
const decoded = jwt.verify(token, privateKey);
if (decoded && decoded.iss === code) {
res.json({
code: 0,
msg: "验证通过"
});
return;
}
res.json({
code: 0,
msg: "验证失败"
});
} catch (err) {
res.json({
code: 1,
msg: err.message
});
}
});
app.get("/genCode", (req, res) => {
const code = (Math.random() * 1000000).toString().substr(0, 4);
const token = jwt.sign({ iss: code, }, privateKey);
const captcha = new captchapng(80, 30, code);
captcha.color(0, 0, 0, 0);
captcha.color(80, 80, 80, 255);
const img = captcha.getBase64();
const imgbase64 = Buffer.from(img, 'base64');
fs.writeFileSync(path.resolve(__dirname, "public/captcha.png"), imgbase64);
res.json({
code: 0,
token,
imgUrl: '/captcha.png'
});
});
app.get('/', (req, res) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type,x-access-token');
res.sendFile(path.resolve(__dirname, "public/index.html"));
});
app.listen(8090, () => {
console.log(`app is listen on 8090`);
});