Grunt Connect Web Server

5.0.1 · active · verified Tue Apr 21

grunt-contrib-connect is a Grunt plugin designed to start a static web server using the `connect` middleware framework. It is currently at stable version 5.0.1, with recent updates, including a major version bump (v5.0.0) in early 2024, indicating an active, though mature, maintenance cycle. This plugin enables developers to configure server parameters such as port, hostname, protocol (HTTP, HTTPS, or HTTP2), and a flexible base directory for serving files, including support for multiple roots and `serve-static` options. Its primary differentiation is its tight integration into the Grunt build system, facilitating seamless local development servers or serving static assets during build processes, often used in conjunction with other Grunt tasks like `grunt-contrib-qunit` for testing. The plugin wraps the popular `connect` server, providing a familiar API within the Grunt ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to install `grunt-contrib-connect`, load it in your Gruntfile, and configure two `connect` tasks: one for development with live reload and automatic browser opening, and another for testing that keeps the server alive indefinitely. It also shows a basic `grunt-contrib-watch` setup, a common companion to `connect` for modern web development workflows.

// Gruntfile.js
module.exports = function(grunt) {
  // Load the plugin that provides the 'connect' task.
  grunt.loadNpmTasks('grunt-contrib-connect');
  grunt.loadNpmTasks('grunt-contrib-watch'); // Often used alongside connect

  grunt.initConfig({
    connect: {
      options: {
        port: 9000,
        hostname: 'localhost', // '0.0.0.0' for external access
        livereload: 35729 // Set a specific port for live reload
      },
      dev: {
        options: {
          open: true, // Automatically open the browser
          base: 'app' // Serve files from the 'app' directory
        }
      },
      test: {
        options: {
          port: 9001,
          base: ['temp', 'test'], // Serve from multiple directories
          keepalive: true // Keep server alive for continuous testing
        }
      }
    },

    watch: {
      options: {
        livereload: '<%= connect.options.livereload %>'
      },
      files: [
        'app/**/*.{html,css,js}',
        '!app/vendor/**/*.js' // Ignore vendor files
      ],
      tasks: [] // No tasks, just trigger livereload
    }
  });

  // Register a default task that runs connect and watch
  grunt.registerTask('default', ['connect:dev', 'watch']);
  grunt.registerTask('serve-test', ['connect:test']);
};

view raw JSON →