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-middleware-proxy

v1.0.7

Published

grunt-contrib-connect middleware proxy utility

Downloads

3,813

Readme

Grunt Middleware function to Proxy Requests

Provides a http/https proxy as middleware for the grunt-contrib-connect plugin. Supporting proxy over proxy and proxy tunneling.

Build Status Coverage Status

Getting Started

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-middleware-proxy --save-dev

One the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-middleware-proxy');

Adapting the "connect" task

Setup

In your project's Gruntfile, add a section named proxies to your existing connect definition.

Basic Proxy Configuration

grunt.initConfig({
    connect: {
        server: {
            options: {
                port: 9000,
                hostname: 'localhost',
                middleware: function (connect, options, middlewares) {
                    /*Requires the Middleware snipped from the Library
                    and add it before the other Middlewares.*/
					middlewares.unshift(require('grunt-middleware-proxy/lib/Utils').getProxyMiddleware());
					return middlewares;
			    }
            },
            proxies: [{
                    context: '/yourapi', //REQUIRED! Must start with a '/' should not end with a '/'
                    host: 'api_server_domain.com', //REQUIRED! Should not contain 'http://' or 'https://'
                    port: 8080, //Optional, defaults to 80 if http or 443 if https
                    https: false,//Optional, defaults to false
                    rewriteHost: true,//Optional, defaults to true
                    auth: 'username:password', //Optional, adds the Authorization header
                    headers: {//Optional.
                        'header':'value'
                    }
            }]
        }
    }
})

Proxy over Proxy Configuration

grunt.initConfig({
    connect: {
        server: {
            options: {
                port: 9000,
                hostname: 'localhost',
                middleware: function (connect, options, middlewares) {
                    /*Requires the Middleware snipped from the Library
                    and add it before the other Middlewares.*/
					middlewares.unshift(require('grunt-middleware-proxy/lib/Utils').getProxyMiddleware());
					return middlewares;
			    }
            },
            proxies: [{
                    context: '/yourapi', //REQUIRED! Must start with a '/' should not end with a '/'
                    host: 'api_server_domain.com', //REQUIRED! Should not contain 'http://' or 'https://'
                    port: 8080, //Optional, defaults to 80 if http or 443 if https
                    https: false,//Optional, defaults to false
                    rewriteHost: true,//Optional, defaults to true
                    /*auth is not supported*/
                    headers: {//Optional.
                        'header':'value'
                    },
                    proxy: {
                        host: 'proxy', //REQUIRED! Should not contain 'http://' or 'https://'
                        port: 8080,//Optional, defaults to 80 if http or 443 if https
                        https: false//Optional, defaults to false
                        /*proxy-specific headers are not supported as they are merged on the request`s header*/
                        /*auth is not supported*/
                    }
            }]
        }
    }
})

Proxy tunneling Configuration

grunt.initConfig({
    connect: {
        server: {
            options: {
                port: 9000,
                hostname: 'localhost',
                middleware: function (connect, options, middlewares) {
                    /*Requires the Middleware snipped from the Library
                    and add it before the other Middlewares.*/
					middlewares.unshift(require('grunt-middleware-proxy/lib/Utils').getProxyMiddleware());
					return middlewares;
			    }
            },
            proxies: [{
                    context: '/yourapi', //REQUIRED! Must start with a '/' should not end with a '/'
                    host: 'api_server_domain.com', //REQUIRED! Should not contain 'http://' or 'https://'
                    port: 8080, //Optional, defaults to 80 if http or 443 if https
                    https: false,//Optional, defaults to false
                    rewriteHost: true,//Optional, defaults to true
                    auth: 'username:password', //Optional, adds the Authorization header
                    headers: {//Optional.
                        'header':'value'
                    },
                    proxyTunnel: {
                        host: 'proxy', //REQUIRED! Should not contain 'http://' or 'https://'
                        port: 8080,//Optional, defaults to 80 if http or 443 if https
                        https: false,//Optional, defaults to false
                        auth: 'username:password', //Optional, adds the Authorization header
                        headers: {//Optional.
                            'header':'value'
                        },
                    }
            }]
        }
    }
})

Registering the Grunt Task

grunt.registerTask('serve', [
	'setupProxies:server',
	'connect:server',
    'watch:default'
]);

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

Options

The available configuration options from a given proxy based on the node http and https modules

options.context

Type: String

The context(s) 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.https

Type: Boolean Default: false

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

options.rewriteHost

Type: Boolean Default: true

If the proxy should rewrite the host header to the target host. If your use case depends on proxying to localhost servers with port, try setting rewriteHost to false.

options.port

Type: Number

If not provided by the configuration, will be defaulted to 80 if https:false or 443 if https:true

The port to proxy to.

options.auth

Type: String

auth : 'username:password'

Creates a new Authorization header on the request as defined by the Standard on HTTP. Basically creates:

headers : {
   'Authorization' : 'Basic ' + btoa('username:password')
}

#### options.headers
>Type: `Object`

>A map of headers to be added to proxied requests.

>```js
>headers: {
>    'header':'value',
>    'anotherheader':'anothervalue'
>}
>```

### Options.proxy

#### options.proxy.host
>Type: `String`

>The host to proxy to. Should not start with the http/https protocol.

#### options.proxy.https
>Type: `Boolean`
>Default: false

>If the proxy should target a https end point on the destination server

#### options.proxy.port
>Type: `Number`

>If not provided by the configuration, will be defaulted to 80 if `https:false` or 443 if `https:true`

>The port to proxy to.

### Options.proxyTunnel

#### options.proxyTunnel.host
>Type: `String`

>The host to proxy to. Should not start with the http/https protocol.

#### options.proxyTunnel.https
>Type: `Boolean`
>Default: false

>If the proxy should target a https end point on the destination server

#### options.proxyTunnel.port
>Type: `Number`

>If not provided by the configuration, will be defaulted to 80 if `https:false` or 443 if `https:true`

>The port to proxy to.

#### options.proxyTunnel.auth
>Type: `String`

>```auth : 'username:password'```

>Creates a new Authorization header on the request as defined by the Standard on HTTP. Will be added to the Request for tunneling.
>Basically creates:

>```js
>headers : {
>    'Authorization' : 'Basic ' + new Buffer('username:password').toString('base64')
>}

options.proxyTunnel.headers

Type: Object

A map of headers to be added to the tunneling request (CONNECT).

headers: {
   'header':'value',
   'anotherheader':'anothervalue'
}