-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
69 lines (60 loc) · 2.04 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
const axios = require('axios')
const body = require('body-parser')
const bodyParser = require('body-parser')
const cors = require('cors')
const express = require('express')
const http = require('http')
const morgan = require('morgan')
const FormData = require('form-data')
const { API_HOST } = require('./config')
const app = new express()
const server = http.createServer(app)
app.use(body.json())
app.use(body.urlencoded({ extended: true }))
app.use(morgan('dev'))
app.use(cors({
allowedHeaders: ['Authorization', 'Content-Type'],
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'],
origin: '*'
}))
app.use('/multipart', bodyParser.json(), function(req, res) {
const data = Object.keys(req.body).reduce((result, prop) => {
result.append(prop, req.body[prop])
return result
}, new FormData())
axios.post(API_HOST + req.path, data, { headers: { ...data.getHeaders(), withCredentials: true } })
.then(response => {
const [token] = response.headers['set-cookie']
return res.json({
...response.data,
token
})
})
.catch(error => {
console.log(require('util').inspect(error.response.data, false, 5, true))
res.statusMessage = error.message
return res.status(error.response.status || 500).end()
})
})
app.use('/', bodyParser.json(), function(req, res) {
const token = req.headers['authorization']
const options = {
data: req.body,
headers: {
...req.headers,
Cookie: token
},
method: req.method,
url: API_HOST + req.path,
withCredentials: true
}
axios(options)
.then(response => res.json(response.data))
.catch(error => {
console.log(require('util').inspect(error.response.data, false, 5, true))
res.statusMessage = error.message
return res.status(error.response.status || 500).end()
})
})
app.disable('x-powered-by')
server.listen(process.env.PORT || 5000)