UNPKG

3.03 kBMarkdownView Raw
1[Grunt homepage](https://github.com/cowboy/grunt) | [Documentation table of contents](toc.md)
2
3# server (built-in task)
4Start a static web server.
5
6## About
7
8This 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.
9
10_Need some help getting started with grunt? Visit the [getting started](getting_started.md) page. And if you're creating your own tasks or helpers, be sure to check out the [types of tasks](types_of_tasks.md) page as well as the [API documentation](api.md)._
11
12## A Very Important Note
13Your `grunt.js` 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.
14
15```javascript
16module.exports = function(grunt) {
17 // Your grunt code goes in here.
18};
19```
20
21## Project configuration
22
23This example shows a brief overview of the [config](api_config.md) properties used by the `server` task. For a more in-depth explanation, see the usage examples.
24
25```javascript
26// Project configuration.
27grunt.initConfig({
28 // Configuration options.
29 server: {}
30});
31```
32
33## Usage examples
34
35### Basic Use
36
37In this example, `grunt server` 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.
38
39The `server` task is most useful when used in conjunction with another task, like the [qunit](task_qunit.md) task.
40
41```javascript
42// Project configuration.
43grunt.initConfig({
44 server: {
45 port: 8000,
46 base: '.'
47 }
48});
49```
50
51### Roll Your Own
52
53Unlike the previous example, in this example the `grunt server` command will run a completely custom `server` 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.
54
55Like 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.
56
57```javascript
58// Project configuration.
59grunt.initConfig({
60 // This custom server task doesn't care about config options!
61});
62
63// Of course, you need to have the "connect" Npm module installed locally
64// for this to work. But that's just a matter of running: npm install connect
65var connect = require('connect');
66
67// Redefining the "server" task for this project. Note that the output
68// displayed by --help will reflect the new task description.
69grunt.registerTask('server', 'Start a custom static web server.', function() {
70 log.writeln('Starting static web server in "www-root" on port 1234.');
71 connect(connect.static('www-root')).listen(1234);
72});
73```
74
75See the [server task source](../tasks/server.js) for more information.