-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
56 lines (45 loc) · 1.42 KB
/
main.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
const express = require('express');
var bodyParser = require('body-parser')
const needle = require('needle');
var cors = require('cors');
const app = express();
app.use(cors({
origin: '*'
}));
app.use(bodyParser.text());
const PORT = process.env.PORT || 5000
app.get('/', (req, res) => {
res.send({ message: 'You should to use post request:)' });
});
app.post('/', (req, res) => {
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain')
res.setHeader('Access-Control-Allow-Origin', '*');
let oParsedData = JSON.parse(req.body);
let method = oParsedData["method"] || "GET";
let targetUrl = oParsedData["target"];
let data = oParsedData["data"] || {};
let options = {
"headers": oParsedData["headers"] || {}
}
if (!targetUrl) {
res.end("Bad Request");
return;
}
needle(method, targetUrl, data, options)
.then((target_response) => {
console.log(`Status: ${target_response.statusCode}`);
console.log('Body: ', target_response.body);
res.statusCode = target_response.statusCode;
res.setHeader('Access-Control-Allow-Origin', '*');
res.statusText = JSON.stringify(target_response.body);
res.setHeader('Content-Type', 'text/plain');
res.end(JSON.stringify(target_response.body));
}).catch((err) => {
res.end("Error of request");
console.error(err);
});
});
app.listen(PORT, () => {
console.log('We are live on ' + PORT);
});