Start a static web server.
Note that this plugin has not yet been released, and only works with the latest bleeding-edge, in-development version of grunt. See the When will I be able to use in-development feature 'X'? FAQ entry for more information.
If you haven't used grunt before, be sure to check out the Getting Started guide.
From the same directory as your project's Gruntfile and package.json, install this plugin with the following command:
npm install grunt-contrib-connect --save-dev
Once that's done, add this line to your project's Gruntfile:
grunt.loadNpmTasks('grunt-contrib-connect');
If the plugin has been installed correctly, running grunt --help
at the command line should list the newly-installed plugin's task or tasks. In addition, the plugin should be listed in package.json as a devDependency
, which ensures that it will be installed whenever the npm install
command is run.
Grunt homepage | Documentation table of contents
Start a static web server.
This task starts a static web server on a specified port, at a specified path, which runs as long as grunt is running. Once grunt's tasks have completed, the web server stops.
Need some help getting started with grunt? Visit the getting started page. And if you're creating your own tasks, be sure to check out the types of tasks page as well as the API documentation.
Your Gruntfile must contain this code, once and only once. If it doesn't, grunt won't work. For the sake of brevity, this "wrapper" code has been omitted from all examples on this page, but it needs to be there.
module.exports = function(grunt) {
// Your grunt code goes in here.
};
This example shows a brief overview of the config properties used by the server
task. For a more in-depth explanation, see the usage examples.
// Project configuration.
grunt.initConfig({
// Configuration options.
connect: {}
});
Type: Integer
Default: 8000
Type: String
Default: localhost
Type: String
Default: .
Base directory.
Type: Boolean
Default: false
Keep the server alive after the task has finished.
Type: Function
Default:
function(connect, options) {
return [
connect.static(options.base),
connect.directory(options.base)
];
}
Lets you to add in your own Connect middlewares.
The option expects a function that returns and array of middlewares. See example in the Gruntfile.
In this example, grunt connect
will start a static web server at http://localhost:8000/
, with its base path set to the Gruntfile's directory. Of course, it will then immediately stop serving files, because grunt exits automatically when there are no more tasks to run.
The connect
task is most useful when used in conjunction with another task, like the qunit task.
// Project configuration.
grunt.initConfig({
connect: {
port: 8000,
base: '.'
}
});
Unlike the previous example, in this example the grunt connect
command will run a completely custom connect
task, because it has been overridden. This version is hard-coded to start a static web server at http://localhost:1234/
, with its base path set to www-root
subdirectory.
Like the previous example, it will then immediately stop serving files, because grunt exits automatically when there are no more tasks to run, but you'll undoubtedly be running additional tasks after this one.
// Project configuration.
grunt.initConfig({
// This custom server task doesn't care about config options!
});
// Of course, you need to have the "connect" Npm module installed locally
// for this to work. But that's just a matter of running: npm install connect
var connect = require('connect');
// Redefining the "connect" task for this project. Note that the output
// displayed by --help will reflect the new task description.
grunt.registerTask('connect', 'Start a custom static web server.', function() {
grunt.log.writeln('Starting static web server in "www-root" on port 1234.');
connect(connect.static('www-root')).listen(1234);
});
See the connect task source for more information.
(Nothing yet)
-- Task submitted by "Cowboy" Ben Alman.
Generated on Wed Oct 31 2012 22:01:31.