grunt-connect-proxy
Provides an http proxy as middleware for the grunt-contrib-connect plugin.
Getting Started
This plugin requires Grunt ~0.4.1
If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
npm install grunt-connect-proxy --save-dev
One the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
grunt.loadNpmTasks('grunt-connect-proxy');
Adapting the "connect" task
Overview
Proxy Configuration
In your project's Gruntfile, add a section named proxies
to your existing connect definition.
grunt.initConfig({
connect: {
options: {
port: 9000,
hostname: 'localhost'
},
proxies: [
{
context: '/cortex',
host: '10.10.2.202',
port: 8080,
https: false,
changeOrigin: false
}
]
}
})
Adding the middleware
Expose the proxy function to use in the middleware, at the top of the grunt file:
var proxySnippet = require('grunt-connect-proxy/lib/utils').proxyRequest;
Add the middleware call from the connect option middleware hook
connect: {
livereload: {
options: {
middleware: function (connect) {
return [
proxySnippet
];
}
}
}
}
Adding the configureProxy task to the server task
For the server task, add the configureProxies task before the connect task
grunt.registerTask('server', function (target) {
grunt.task.run([
'clean:server',
'compass:server',
'configureProxies',
'livereload-start',
'connect:livereload',
'open',
'watch'
]);
});
Options
The available configuration options from a given proxy are generally the same as what is provided by the underlying httpproxy library
options.context
Type: String
The context to match requests against. Matching requests will be proxied. Should start with /. Should not end with /
options.host
Type: String
The host to proxy to. Should not start with the http/https protocol.
options.port
Type: Number
Default: 80
The port to proxy to.
options.https
Type: Boolean
Default: false
Whether to proxy with https
options.changeOrigin
Type: Boolean
Default: false
Whether to change the origin on the request to the proxy, or keep the original origin.
options.appendProxies
Type: Boolean
Default: true
Set to false to isolate multi-task configuration proxy options from parent level instead of appending them.
Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.
Multi-server proxy configuration
grunt-contrib-connect multi-server configuration is supported. You can define proxies blocks in per-server options and refer to those blocks in task invocation.
grunt.initConfig({
connect: {
options: {
port: 9000,
hostname: 'localhost'
},
server2: {
proxies: [
{
context: '/cortex',
host: '10.10.2.202',
port: 8080,
https: false,
changeOrigin: false
}
]
},
server3: {
appendProxies: false,
proxies: [
{
context: '/api',
host: 'example.org'
}
]
}
}
})
grunt.registerTask('e2etest', function (target) {
grunt.task.run([
'configureProxies:server2',
'open',
'karma'
]);
});
Release History
0.1.0 Initial release
0.1.1 Fix changeOrigin
0.1.2 Support multiple server definitions, bumped to grunt 0.4.1