Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including grunt-connect-delay with all npm packages installed. Try it out:

require("grunt/package.json"); // grunt is a peer dependency. var gruntConnectDelay = require("grunt-connect-delay")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

grunt-connect-delay v0.2.4

Provides a delay proxy for grunt connect.

grunt-connect-delay Build Status

A delay proxy middleware for grunt-contrib-connect

This plugin provides a delay middleware for Grunt Connect / Express. It can be used for introducing artificial delays before proxying certain URLs based on RegExp rules.

By default, it includes the rule that any URL which matches the pattern ^/delay/([0-9]+)/(.*)$ will be delayed by $1 milliseconds and be redirected to /$2.

Hence, to check your app's behavior when a particular request is slow, just prefix /delay/5000/ to the URL.

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.

npm install grunt-connect-delay --save-dev



Type: Boolean

Default Value: true

This adds the following default rule to the configuration:

{ rule    : '/delay/([0-9])+/(.*)apos;
, delay   : '$1'
, rewrite : '/$2'

This means that to delay a link, you only need to prefix it with /delay/:milliseconds/<original URL>. grunt-connect-delay will wait for the said number of milliseconds before proceeding to the <original URL>


Type: String

Default value: connect.delay

This is the section in the Gruntfile from where the delay rules will be read.

Simple rule format

{url: 'FROM', delay: MILLISECONDS }


  • FROM: RegExp string to match
  • MILLISECONDS: The delay in milliseconds to introduce before proceeding with proxying
Extended rule format



  • FROM_REGEXP: RegExp string to match the URL and create groups to be used for deducing delay and the true proxied URL. e.g. ^/delay/([0-9]*)/(.*)$.
  • NUMBER_OR_REPLACEMENT: The number of milliseconds (e.g. 1000) to wait or a string which can be parsed to an integer after a URL (which matches url) is replaced by it (e.g. $1).
  • REPLACEMENT: A string which maps to the destination URL after a URL is replaced by it (e.g. /$2).


In your project's Gruntfile:

  • Include the delayRequest snippet: var delayRequest = require('grunt-connect-delay/lib/utils').delayRequest.
  • Load the plugin: grunt.loadNpmTasks('grunt-connect-delay')
  • Add configureDelayRules before the web server task.

Optional (advanced usage):

  • Configure options in configureDelayRules.options in initConfig, or,
  • Add a section named delay to your existing Connect definition

To see some logs about the proxying, use the --verbose flag while running grunt.

var delayRequestSnippet = require('grunt-connect-delay/lib/utils').delayRequest;
    connect: {
        delay: [
            { url: '^/api/.*apos;, delay: 10000 } // Delay calls to API by 10sec
        dev: {
            options: {
                middleware: function (connect, options, middlewares) {
                    return middlewares;

                    // Old style:
                    // See:
                    // return [
                    //        delayRequestSnippet
                    //      , rewriteRequestSnippet
                    //      , proxyRequestSnippet
                    //      , connect.static(path.resolve(dir))
                    // ];
    /* Optional */
    configureDelay: {
        options: {
            rulesProvider  : 'connect.delay'
          , useDefaultRule : true


// "configureDelayRules" should be before the "connect"/"express" task
grunt.registerTask('server', function (target) {[

Though I haven't tested it, it should work in a similar fashion with grunt-express task as well.

You can see how to use this plugin at musically-ut/grunt-connect-delay-example.


The structure and documentation of this plugin is inspired by grunt-connect-rewrite plugin.

Release Notes

  • 0.2.3: Update usage instuctions
  • 0.2.2: Bump version to resolve npm bug
  • 0.2.1: Improve documentation
  • 0.2.0: Add extended rules and a default rule
  • 0.1.1: Improve logging
  • 0.1.0: Initial release
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free