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-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()
|
|
|
|
}
|
2018-03-05 00:30:17 +00:00
|
|
|
console.log('xhr request', req.originalUrl)
|
2018-03-04 02:49:11 +00:00
|
|
|
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,
|
|
|
|
host: process.env.API
|
|
|
|
},
|
|
|
|
followRedirect: false
|
|
|
|
})
|
|
|
|
)
|
2018-03-05 00:30:17 +00:00
|
|
|
.on('error', console.log)
|
2018-03-04 02:49:11 +00:00
|
|
|
.pipe(res)
|
|
|
|
}
|
|
|
|
|
2018-03-04 03:51:16 +00:00
|
|
|
module.exports = apiProxyMiddleware
|