Skip to content

Switch from express-http-proxy to node-http-proxy, proxy to [::1]:3000 #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ The Self Service UI for the [ManageIQ](http://github.com/ManageIQ/manageiq) proj
- In the `manageiq-ui-self_service` directory, start the development version of
the self service UI with `gulp serve-dev`, which will start the UI listening
on http://localhost:3001, and talking to the REST API at
http://localhost:3000. This command will also open a browser page to
http://localhost:3001/self_service/login .
http://[::1]:3000. This command will also open a browser page to
http://localhost:3001/self_service/login.
The REST API host can be overriden via a PROXY\_HOST environment variable, for
example: `PROXY_HOST=127.0.0.1:3000 gulp serve-dev`.

## Deployment

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"dateformat": "^1.0.11",
"debug": "^2.1.3",
"del": "^2.0.2",
"express-http-proxy": "^0.6.0",
"glob": "^5.0.3",
"gulp": "^3.8.11",
"gulp-angular-gettext": "^2.1.0",
Expand Down Expand Up @@ -92,6 +91,7 @@
"dependencies": {
"body-parser": "^1.12.2",
"express": "^4.12.3",
"http-proxy": "^1.13.2",
"morgan": "^1.5.2",
"serve-favicon": "^2.2.0"
},
Expand Down
38 changes: 31 additions & 7 deletions server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'use strict';

var express = require('express');
var proxy = require('express-http-proxy');
var httpProxy = require('http-proxy');
var app = express();
var router = express.Router();
var bodyParser = require('body-parser');
Expand All @@ -13,15 +13,33 @@ var four0four = require('./utils/404')();
var url = require('url');

var environment = process.env.NODE_ENV;
var PROXY_HOST = process.env.PROXY_HOST || '[::1]:3000';

app.use('/api', proxy('127.0.0.1:3000', {
forwardPath: function(req, res) {
var path = '/api' + url.parse(req.url).path;
var PROXY_TARGET = 'http://' + PROXY_HOST + '/api';
var proxy_error_handler = function(req, res) {
return function(err, data) {
if (!err)
return;

console.log('PROXY: http://127.0.0.1:3000' + path);
return path;
res.writeHead(500, {
'Content-Type': 'text/plain'
});

res.end('Something went wrong: ' + err);
console.error(err);
}
}));
}

var proxy = httpProxy.createProxyServer({
target: PROXY_TARGET,
});

app.use('/api', function(req, res) {
var path = url.parse(req.url).path;

console.log('PROXY: ' + PROXY_TARGET + path);
proxy.web(req, res, proxy_error_handler(req, res));
});

router.use(favicon(__dirname + '/favicon.ico'));
router.use(bodyParser.urlencoded({ extended: true }));
Expand Down Expand Up @@ -50,6 +68,12 @@ switch (environment) {
router.use(express.static('./client/'));
router.use(express.static('./tmp'));
router.use(express.static('./client/assets'));
var pictureProxy = httpProxy.createProxyServer({
target: 'http://' + PROXY_HOST + '/pictures',
});
app.use('/pictures', function(req, res) {
pictureProxy.web(req, res, proxy_error_handler(req, res));
});
app.use(express.static('./'));
// Any invalid calls for templateUrls are under app/* and should return 404
router.use('/app/*', function(req, res) {
Expand Down