-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
71 lines (61 loc) · 1.86 KB
/
app.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
const express = require('express');
const http = require('http');
const https = require('https');
const cors = require('cors');
const axios = require('axios');
const path = require('path');
const app = express();
const port = 3000;
const httpServer = http.Server(app);
app.use(cors()); // Habilita el middleware de CORS
app.use(express.json()); // for parsing application/json
app.use(express.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.get('/proxy', (req, res) => {
console.log(req.query);
const url = req.query.url;
https.get(url, (responseFromOtherServer) => {
let rawData = '';
responseFromOtherServer.on('data', (chunk) => {
rawData += chunk;
});
responseFromOtherServer.on('end', () => {
try {
const jsonData = JSON.parse(rawData);
res.json(jsonData);
} catch (e) {
console.error(e.message);
res.status(500).json({ type: 'error', message: e.message });
}
});
}).on('error', (err) => {
res.status(500).json({ type: 'error', message: err.message });
});
});
app.post('/proxy_post', async (req, res) => {
console.log(req.body);
let data = JSON.stringify({
"parameters": req.body.parameters
});
let config = {
method: 'post',
maxBodyLength: Infinity,
url: req.body.url,
headers: {
'Content-Type': 'application/json'
},
data: data
};
axios.request(config)
.then((response) => {
res.json(response.data);
})
.catch((error) => {
res.json(error);
});
});
httpServer.listen(3000, () => {
console.log('go to http://localhost:3000');
});