Skip to content

Commit

Permalink
Added http-proxy allowing paths to proxy out to other servers.
Browse files Browse the repository at this point in the history
This is useful for when your application isn't in a NodeJS stack. For
example if you're building a site with PHP but you want to use grunt-bbb
for all of the javascript development, you can proxy /api out to your
PHP site's /api. When you're ready to deploy you have completed
javascript code that can run on your PHP domain.
  • Loading branch information
AsaAyers committed Oct 16, 2012
1 parent 323565a commit 51da163
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"requirejs": "2.0.6",
"handlebars": "1.0.6-2",
"express": "2.5.9",
"http-proxy": "0.8.3",

"stylus": "0.29.0",
"nib": "0.8.2",
Expand Down
30 changes: 30 additions & 0 deletions tasks/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,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) {
Expand Down

0 comments on commit 51da163

Please sign in to comment.