grunt-connect-proxy
Provides a 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.rejectUnauthorized:
Type: Boolean
Default: false
Whether to reject self-signed certificates when https: true is set. Defaults to accept self-signed certs since proxy is meant for development environments.
options.appendProxies
Type: Boolean
Default: true
Set to false to isolate multi-task configuration proxy options from parent level instead of appending them.
options.rewrite
Type: Object
Allows rewrites of url (including context) when proxying. The object's keys serve as the regex used in the replacement operation. As an example the following proxy configuration will remove the context when proxying:
proxies: [
context: '/context',
host: 'host',
port: 8080,
rewrite: {
'^/removingcontext': '',
'^/changingcontext': '/anothercontext'
}
]
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 (thanks to @lauripiispanen)
- 0.1.3 Bumped http-proxy dependency to 0.10.2
- 0.1.4 Added proxy rewrite support (thanks to @slawrence)
- 0.1.5 Default rejectUnauthorized to false to allow self-signed certificates over SSL