metamaps--metamaps/apiProxyMiddleware.js
2018-03-05 11:31:54 -05:00

22 lines
No EOL
607 B
JavaScript

const request = require('request')
function apiProxyMiddleware (req, res, next) {
if (!(req.xhr || req.originalUrl.indexOf('.json') > -1 || req.method !== 'GET')) {
return next()
}
console.log('xhr request', req.originalUrl)
const method = req.method.toLowerCase()
req.pipe(
request[method](process.env.API + req.originalUrl, {
headers: {
...req.headers,
host: process.env.API
},
followRedirect: false
})
)
.on('error', console.log)
.pipe(res)
}
module.exports = apiProxyMiddleware