-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (47 loc) · 1.38 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
const ARGS = process.argv.slice(2);
if(!ARGS[0] || !ARGS[1])
throw("Informe a porta e a url base para rodar o programa")
const express = require('express')
const fs = require('fs')
const app = express()
const axios = require('axios')
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(bodyParser.urlencoded({ extended: true }));
const port = parseInt(ARGS[0]);
const URL_BASE = ARGS[1];
function formatAsFormData(object){
const entries = Object.entries(object);
return entries.map(([key, value]) => key + "=" + value).join("&");
}
app.use((req, res, next) => {
let { method, headers, path, query, body } = req;
axios({
method,
url: `${URL_BASE}/${path}?${formatAsFormData(query)}`,
data: body ? formatAsFormData(body) : '',
withCredentials: true,
responseType: 'arraybuffer',
headers:{
Cookie: headers.cookie ? headers.cookie : ""
}
})
.then(resInternal => {
let {headers, status, data} = resInternal;
if(headers != null && headers['set-cookie']){
headers['Set-Cookie'] = headers['set-cookie']
}
res.header(headers)
res.status(status ? status : 200)
res.write(data)
res.end();
})
.catch(errorInternal => {
console.log(errorInternal)
res.end(500);
})
})
app.listen(port, () => {
console.log(`Aplicacao rodando na porta: http://localhost:${port}`)
})