Permalink
Browse files

Merge pull request #72 from AsaAyers/master

Added http-proxy allowing paths to proxy out to other servers.
  • Loading branch information...
tbranyen committed Nov 3, 2012
2 parents e20411c + 5e759bb commit 6bf50a966de6c4c2e631f7af3703055d50eadb5b
Showing with 31 additions and 0 deletions.
  1. +1 −0 package.json
  2. +30 −0 tasks/server.js
View
@@ -23,6 +23,7 @@
"requirejs": "2.1.1",
"handlebars": "1.0.7",
"express": "2.5.9",
+ "http-proxy": "0.8.3",
"stylus": "0.30.1",
"nib": "0.8.2",
View
@@ -124,6 +124,36 @@ module.exports = function(grunt) {
// Serve favicon.ico.
site.use(express.favicon(options.favicon));
+
+ /**
+ * Allows the server to proxy a URL to bypass Same Origin Policies for
+ * easier API testing.
+ *
+ * proxies everything under /api out to http://host_running_api.local/api
+ * server: {
+ * proxies: {
+ * 'api': {
+ * host: 'host_running_api.local',
+ * port: 80, // optional
+ * https: false // optional
+ * }
+ * }
+ * }
+ */
+ if (_.isObject(options.proxies)) {
+ httpProxy = require('http-proxy');
+ Object.keys(options.proxies).sort().reverse().forEach(function(key) {
+
+ proxy = new httpProxy.HttpProxy({
+ changeOrigin: true,
+ target: options.proxies[key]
+ })
+
+ site.all(root + key + '/*', function(req, res) {
+ proxy.proxyRequest(req, res)
+ })
+ });
+ }
// Ensure all routes go home, client side app..
site.all("*", function(req, res) {

0 comments on commit 6bf50a9

Please sign in to comment.