2018-03-04 03:51:16 +00:00
|
|
|
const request = require('request')
|
2018-03-04 02:49:11 +00:00
|
|
|
|
|
|
|
function apiProxyMiddleware (req, res, next) {
|
2018-03-09 20:53:46 +00:00
|
|
|
// TODO: tidy this up!
|
2018-03-10 00:17:56 +00:00
|
|
|
if (!(req.xhr || req.headers['content-type'] === 'application/json' || req.originalUrl.indexOf('.json') > -1 || req.method !== 'GET')) {
|
2018-03-04 02:49:11 +00:00
|
|
|
return next()
|
|
|
|
}
|
|
|
|
const method = req.method.toLowerCase()
|
|
|
|
req.pipe(
|
2018-03-04 03:51:16 +00:00
|
|
|
request[method](process.env.API + req.originalUrl, {
|
2018-03-04 02:49:11 +00:00
|
|
|
headers: {
|
|
|
|
...req.headers,
|
2018-03-06 04:27:38 +00:00
|
|
|
cookie: `_Metamaps_session=${req.cookies._Metamaps_session}`,
|
|
|
|
host: 'localhost:3001'
|
2018-03-04 02:49:11 +00:00
|
|
|
},
|
|
|
|
followRedirect: false
|
|
|
|
})
|
|
|
|
)
|
2018-03-06 04:27:38 +00:00
|
|
|
.on('error', (err) => console.log(err))
|
2018-03-04 02:49:11 +00:00
|
|
|
.pipe(res)
|
|
|
|
}
|
|
|
|
|
2018-03-04 03:51:16 +00:00
|
|
|
module.exports = apiProxyMiddleware
|