metamaps--metamaps/apiProxyMiddleware.js

22 lines
652 B
JavaScript
Raw Normal View History

const request = require('request')
2018-03-04 02:49:11 +00:00
function apiProxyMiddleware (req, res, next) {
2018-03-05 16:31:54 +00:00
if (!(req.xhr || 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(
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)
}
module.exports = apiProxyMiddleware