-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
105 lines (91 loc) · 2.67 KB
/
Copy pathapp.js
File metadata and controls
105 lines (91 loc) · 2.67 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//引入模块
const express = require('express')
const app = express()
const routes = require('./routes/index')
const routesAdmin = require('./routes/admin')
const routesHall = require('./routes/hall')
const jwt = require('jsonwebtoken')
const fs = require('fs')
const cors = require('cors')
// http https
const http = require('http')
const https = require('https')
const options = {
key: fs.readFileSync('utils/ssl/2114558_api.xuewuzhijing.top.key'),
cert: fs.readFileSync('utils/ssl/2114558_api.xuewuzhijing.top.pem')
}
// 设置允许跨域访问该服务
// app.all('*', function (req, res, next) {
// res.header('Access-Control-Allow-Origin', '*');
// res.header('Access-Control-Allow-Headers', 'Content-Type,x-access-token');
// res.header('Access-Control-Allow-Methods', '*');
// res.header('Content-Type', 'application/json;charset=utf-8');
// if (req.method == 'OPTIONS') {
// res.send(200);
// }
// else {
// next();
// }
// });
//post请求解析
const bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: false}))
app.use(cors())
//不需要登录的接口
const noLogin = ['/weChatApp/login', '/admin/QrLogin',
'/hall/LoginPcConfirm', '/admin/LoginStatus',
'/hall/scanningQr','/admin/statusQr','/admin/Upload']
//中间件拦截请求
app.use((req, res, next) => {
let isLogin = noLogin.some(function (item) {
return req.path === item
})
//跳过登录
if (isLogin) {
next()
} else {
//从不同的方式拿token
var token = req.body.token || req.query.token || req.headers['x-access-token'];
if (token) {
//验证token
jwt.verify(token, 'xiaoyue', function (err, decoded) {
if (err) {
res.json({
code: 400,
data: {
msg: '无效的token'
}
})
} else {
req.decoded = decoded
next()
}
})
} else {
//没有带token
res.json({
code: 400,
data: {
msg: '没有登录!'
}
})
}
}
})
//捕获请求
app.use('/weChatApp', routes) //小程序
app.use('/admin', routesAdmin) //admin
app.use('/hall', routesHall) //hall
// 监听端口
// app.listen(2333)
http.createServer(app).listen(2333)
// https.createServer(options, app).listen(443)
/**
* 响应码:
* 200 ok
* -200 error
* 400 无效的token
* 401 无权限
* 300 catch捕获的错误
*/