-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathserver.js
99 lines (91 loc) · 2.63 KB
/
server.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
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
const express = require('express');
const app = express();
const path = require('path');
const fs = require('fs');
const bodyParser = require('body-parser');
const coBody = require('co-body');
// 创建静态服务
const serveStatic = require('serve-static');
const rootPath = path.join(__dirname, 'dist');
app.use(serveStatic(rootPath));
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true, parameterLimit: 50000 }));
app.all('*', function (res, req, next) {
req.header('Access-Control-Allow-Origin', '*');
req.header('Access-Control-Allow-Headers', 'Content-Type');
req.header('Access-Control-Allow-Methods', '*');
req.header('Content-Type', 'application/json;charset=utf-8');
next();
});
// 存储性能数据
let performanceList = [];
// 存储错误数据
let errorList = [];
// 存储录屏数据
let recordScreenList = [];
// 存储白屏检测数据
let whiteScreenList = [];
// 获取js.map源码文件
app.get('/getmap', (req, res) => {
// req.query 获取接口参数
let fileName = req.query.fileName;
let mapFile = path.join(__filename, '..', 'dist/js');
// 拿到dist目录下对应map文件的路径
let mapPath = path.join(mapFile, `${fileName}.map`);
fs.readFile(mapPath, function (err, data) {
if (err) {
console.error(err);
return;
}
res.send(data);
});
});
app.get('/getErrorList', (req, res) => {
res.send({
code: 200,
data: errorList
});
});
app.get('/getRecordScreenId', (req, res) => {
let id = req.query.id;
let data = recordScreenList.filter((item) => item.recordScreenId == id);
res.send({
code: 200,
data
});
});
app.post('/reportData', async (req, res) => {
try {
// req.body 不为空时为正常请求,如录屏信息
let length = Object.keys(req.body).length;
if (length) {
recordScreenList.push(req.body);
} else {
// 使用 web beacon 上报数据
let data = await coBody.json(req);
if (!data) return;
if (data.type == 'performance') {
performanceList.push(data);
} else if (data.type == 'recordScreen') {
recordScreenList.push(data);
} else if (data.type == 'whiteScreen') {
whiteScreenList.push(data);
} else {
errorList.push(data);
}
}
res.send({
code: 200,
meaage: '上报成功!'
});
} catch (err) {
res.send({
code: 203,
meaage: '上报失败!',
err
});
}
});
app.listen(8083, () => {
console.log('Server is running at http://localhost:8083');
});