add apiProxyMiddleware

This commit is contained in:
Connor Turland 2018-03-03 21:49:11 -05:00
parent db2ca5945c
commit 562da81a0f
2 changed files with 29 additions and 4 deletions

21
apiProxyMiddleware.js Normal file
View file

@ -0,0 +1,21 @@
import request from 'request'
function apiProxyMiddleware (req, res, next) {
if (true) { // is application/json
return next()
}
console.log(req.get('Content-Type'))
const method = req.method.toLowerCase()
req.pipe(
request[method](url, {
headers: {
...req.headers,
host: process.env.API
},
followRedirect: false
})
)
.pipe(res)
}
export default apiProxyMiddleware

View file

@ -2,6 +2,8 @@ const path = require('path')
const express = require('express')
const webpack = require('webpack')
const webpackDevMiddleware = require('webpack-dev-middleware')
const apiProxyMiddleware = require('./apiProxyMiddleware')
const port = process.env.PORT || 3000
const app = express();
@ -13,13 +15,15 @@ const compiler = webpack(config);
// configuration file as a base.
app.use(webpackDevMiddleware(compiler, {
publicPath: config.output.publicPath
}));
}))
app.use(apiProxyMiddleware)
app.get('*', function (req, res) {
res.sendFile(path.join(__dirname, 'public/index.html'))
})
// Serve the files on port 3000.
app.listen(3000, function () {
console.log('Metamaps listening on port 3000!\n');
// Serve the files on set port or port 3000.
app.listen(port, function () {
console.log('Metamaps listening on port ' + port + '\n')
});