add apiProxyMiddleware
This commit is contained in:
parent
db2ca5945c
commit
562da81a0f
2 changed files with 29 additions and 4 deletions
21
apiProxyMiddleware.js
Normal file
21
apiProxyMiddleware.js
Normal 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
|
12
server.js
12
server.js
|
@ -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')
|
||||
});
|
Loading…
Reference in a new issue