Skip to content

Instantly share code, notes, and snippets.

@spitimage
Created April 8, 2014 02:37
Show Gist options
  • Save spitimage/10084529 to your computer and use it in GitHub Desktop.
Save spitimage/10084529 to your computer and use it in GitHub Desktop.
Grunt Connect Proxy
'use strict';
// proxy-middleware from https://github.com/andrewrk/connect-proxy
var proxy = require('proxy-middleware')
, url = require('url');
function middleware(connect, options, middlewares) {
var proxyUrl = options.proxy || 'http://localhost:8000';
var proxyOptions = url.parse(proxyUrl);
proxyOptions.route = '/api';
middlewares.push(proxy(proxyOptions));
return middlewares;
}
module.exports = function (grunt) {
// Load grunt tasks automatically
require('load-grunt-tasks')(grunt);
// Time how long tasks take. Can help when optimizing build times
require('time-grunt')(grunt);
// Define the configuration for all the tasks
grunt.initConfig({
// Other grunt config stuff would go here
// Here is the configuration for grunt-connect:
connect: {
options: {
port: 9000,
hostname: 'localhost',
livereload: 35729,
// Add the above proxy to the default connect configuration
middleware: proxy,
// Add the default API destination URL
proxy: 'http://localhost:8000'
},
otherAPI: {
// Add a different API destination URL
proxy: 'http://localhost:8001'
}
}
});
// Register grunt tasks here
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment