metamaps--metamaps/apiProxyMiddleware.js

22 lines
583 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 14:53:45 +00:00
if (!(req.xhr || req.originalUrl.indexOf('.json') > -1)) {
2018-03-04 02:49:11 +00:00
return next()
}
console.log('xhr request', req.originalUrl)
2018-03-04 02:49:11 +00:00
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,
host: process.env.API
},
followRedirect: false
})
)
.on('error', console.log)
2018-03-04 02:49:11 +00:00
.pipe(res)
}
module.exports = apiProxyMiddleware