-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiProxyMiddleware.js
34 lines (30 loc) · 1.04 KB
/
apiProxyMiddleware.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
/*
This file takes appropriate requests from our UI client
and pipes them through to the API, proxying the response
back to the client. To do this, it needs to pass
the _Metamaps_session cookie in particular,
in order to make authorized requests
*/
const request = require('request')
const { API_PROTOCOL, API_HOST } = process.env
const API_URL = `${API_PROTOCOL}://${API_HOST}`
function apiProxyMiddleware (req, res, next) {
// TODO: tidy this up!
if (!(req.xhr || req.headers['content-type'] === 'application/json' || req.originalUrl.indexOf('.json') > -1 || req.method !== 'GET')) {
return next()
}
const method = req.method.toLowerCase()
req.pipe(
request[method](API_URL + req.originalUrl, {
headers: {
...req.headers,
cookie: `_Metamaps_session=${req.cookies._Metamaps_session}`,
host: API_HOST
},
followRedirect: false
})
)
.on('error', (err) => console.log(err))
.pipe(res)
}
module.exports = apiProxyMiddleware