npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

grunt-connect-proxy-updated

v0.2.1

Published

Provides a http proxy as middleware for grunt connect.

Downloads

759

Readme

grunt-connect-proxy-updated

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-updated --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-updated');

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: {
        server: {
            options: {
                port: 9000,
                hostname: 'localhost'
            },
            proxies: [
                {
                    context: '/cortex',
                    host: '10.10.2.202',
                    port: 8080,
                    https: false,
                    xforward: false,
                    headers: {
                        "x-custom-added-header": value
                    },
                    hideHeaders: ['x-removed-header']
                }
            ]
        }
    }
})

Adding the middleware

With Livereload

Add the middleware call from the connect option middleware hook

        connect: {
            livereload: {
                options: {
                    middleware: function (connect, options) {
                        if (!Array.isArray(options.base)) {
                            options.base = [options.base];
                        }

                        // Setup the proxy
                        var middlewares = [require('grunt-connect-proxy-updated/lib/utils').proxyRequest];

                        // Serve static files.
                        options.base.forEach(function(base) {
                            middlewares.push(connect.static(base));
                        });

                        // Make directory browse-able.
                        var directory = options.directory || options.base[options.base.length - 1];
                        middlewares.push(connect.directory(directory));

                        return middlewares;
                    }
                }
            }
        }
Without Livereload

It is possible to add the proxy middleware without Livereload as follows:

   // server
    connect: {
      server: {
        options: {
          port: 8000,
          base: 'public',
          logger: 'dev',
          hostname: 'localhost',
          middleware: function (connect, options, defaultMiddleware) {
             var proxy = require('grunt-connect-proxy-updated/lib/utils').proxyRequest;
             return [
                // Include the proxy first
                proxy
             ].concat(defaultMiddleware);
          }
        },
        proxies: [ /* as defined above */ ]
      }
  }

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:server',
            'livereload-start',
            'connect:livereload',
            'open',
            'watch'
        ]);
    });

IMPORTANT: You must specify the connect target in the configureProxies task.

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 or Array

The context(s) to match requests against. Matching requests will be proxied. Should start with /. Should not end with / Multiple contexts can be matched for the same proxy rule via an array such as: context: ['/api', '/otherapi']

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

if the proxy should target a https end point on the destination server

options.secure

Type: Boolean Default: true

true/false, if you want to verify the SSL Certs (Avoids: SELF_SIGNED_CERT_IN_CHAIN errors when set to false)

Whether to proxy with https

options.xforward:

Type: Boolean Default: false

Whether to add x-forward headers to the proxy request, such as "x-forwarded-for": "127.0.0.1", "x-forwarded-port": 50892, "x-forwarded-proto": "http"

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 update the context when proxying:

proxies: [
    context: '/context',
    host: 'host',
    port: 8080,
    rewrite: {
        '^/removingcontext': '',
        '^/changingcontext': '/anothercontext',
        '^/updating(context)': function(match, p1) {
            return '/new' + p1;
        }
    }
]

options.headers

Type: Object

A map of headers to be added to proxied requests.

options.hostRewrite

Type: String

Rewrites the location hostname on 30X redirects.

options.hideHeaders

Type: Array

An array of headers that should be removed from the server's response.

options.errorHandler

Type: Function

Another middleware that will be called if proxy request fails. Example:

    errorHandler: function(req, res, next, err) {
        if (err.code === 404) {
            res.send('Some error page');
        } else {
            next();
        }
    }

options.ws

Type: Boolean Default: false

Set to true to proxy websockets.

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,
                    }
                ]
            },
            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
  • 0.1.6 Add xforward option, added support for context arrays, added debug logging
  • 0.1.7 Added WebSocket support (thanks for @killfill), Headers support (thanks to @gadr), various docs fixed
  • 0.1.8 Minor websocket bug fix
  • 0.1.10 Minor bug fix
  • 0.1.11 Fix Websocket support on Node 0.10 - Bumped http-proxy dependency to 1.1.4, Removed unsupported http-proxy options (rejectUnauthorized, timeout, changeOrigin)
  • 0.2.0 Added hidden headers support
  • 0.2.1 Added support for Grunt version >= 0.4.1