-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
104 lines (90 loc) · 2.86 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
100
101
102
const express = require('express');
const request = require('request')
const bodyParser = require('body-parser');
const fs = require('fs');
const updateModified = require('./utils/updateModified')
const app = express();
const configs = JSON.parse(fs.readFileSync('./configs.json'))
const { getCookie,getJar } = require('./utils/getCookie')
let jar;
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static('webapp'))
configureEndPoints(configs.endPoints)
app.listen(configs.LocalPort, () => {
console.log('listening at port ' + configs.LocalPort)
getJar().then(_jar=>{
jar = _jar
console.log("go to this url see the ui5 app", "http://localhost:" + configs.LocalPort + "/index.html")
}).catch((err)=>{
console.log(err)
})
})
function configureEndPoints(endPoints) {
//get end points
endPoints.get.forEach(endPoint => {
app.get(endPoint, (req, res) => {
get_data(req.query, endPoint).then(data => {
if (req.query['Content-Type'] === 'text/xml') {
res.set('Content-Type', 'application/xml')
}
res.send(data)
})
})
})
//post end points
endPoints.post.forEach(endPoint => {
app.post(endPoint, (req, res) => {
post_data(req.body, endPoint).then(data => {
if (req.body['Content-Type'] === 'text/xml') {
res.set('Content-Type', 'application/xml')
}
res.send(data)
})
})
})
}
function get_data(query, service) {
return new Promise((resolve, reject) => {
request({
url: `${configs.server}${service}`,
method: 'get',
headers: {
// 'Cookie': cookie,
'Content-Type': (query['Content-Type'] === 'text/json') ? 'application/json' : 'application/xml'
},
'json': (query['Content-Type'] === 'text/json') ? true : false,
qs: query,
jar:jar
}, (error, response, body) => {
if (error) {
reject(response)
} else {
resolve(body)
}
})
})
}
function post_data(query, service) {
return new Promise((resolve, reject) => {
request({
url: `${configs.server}${service}`,
method: 'post',
headers: {
// 'Cookie': cookie,
'Content-Type': (query['Content-Type'] === 'text/json') ? 'application/json' : 'application/xml'
},
'json': (query['Content-Type'] === 'text/json') ? true : false,
qs: query,
jar:jar
}, (error, response, body) => {
if (error) {
reject(response)
} else {
resolve(body)
}
})
})
}
updateModified()