-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (79 loc) · 1.65 KB
/
index.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
const express = require('express')
const app = express()
const port = 3000
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
// in latest body-parser use like below.
app.use(bodyParser.urlencoded({ extended: true }));
const db = require('./util/mongo')
//设置跨域访问
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By",' 3.2.1')
res.header("Content-Type", "application/json;charset=utf-8");
next();
});
app.post('/tracker', async (req, res) => {
try{
const _ = await db.addParms(JSON.parse(Object.keys(req.body)[0]))
res.send({
code: 1,
data: {
id: _
},
msg: 'success'
})
}catch(e)
{
res.send({
code: 0,
data: {},
msg: e
})
}
})
app.get('/tracker', async (req, res) => {
if(req.query.uuid === 'admin') {
req.query = {};
}
try{
const _ = await db.getParms(req.query)
res.send({
code: 1,
data: _,
msg: 'success'
})
}catch(e)
{
res.send({
code: 0,
data: [],
msg: e
})
}
})
app.delete('/tracker', async (req, res) => {
try{
const _ = await db.removeData(req.body)
res.send({
code: 1,
data: {
deleteCount: _
},
msg: 'success'
})
}catch(e)
{
res.send({
code: 0,
data: {},
msg: e
})
}
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})