Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 9108ee81f117c839988d147895c3a0473a7826f5
Author: vladikoff <vlad.filippov@gmail.com>
Date:   Mon Jun 30 19:51:22 2014 -0700

    comma fix, spacing

commit 61ca4c3d5ce40fc7b005711a61fc6c9428a2378c
Author: vladikoff <vlad.filippov@gmail.com>
Date:   Mon Jun 30 19:50:18 2014 -0700

    adjust readme

commit ec8d897fc709d3b72555d492d228fd76429e51ff
Author: vladikoff <vlad.filippov@gmail.com>
Date:   Mon Jun 30 19:47:38 2014 -0700

    style fix

commit d2e4e20d56ade1ceb4b47b8b3dd7f6ddfba6a3c4
Author: Aaron Lampros <alampros@gmail.com>
Date:   Mon Jun 2 09:37:29 2014 -0400

    Revert unnecessary modifications

    Revert CHANGELOG update.
    Revert travis.ci directive change.
    Revert version bump.

commit 8caf1619ef70ce03fc6401c2c483e875d7d53ec4
Author: Aaron Lampros <alampros@gmail.com>
Date:   Wed May 28 13:29:59 2014 -0400

    Add support for connect.static instance options

    Allow specifying `base` as an object to facilitate passing an options object to the connect.static module.
    Add tests for connect.static instance options.
    Update documentation.
    Update change log.

Closes gh-110.
  • Loading branch information
alampros authored and vladikoff committed Jul 1, 2014
1 parent b96ae23 commit 9dbea9f
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 11 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ Eddie Monge Jr
Christopher Joslyn
Ates Goral
Alex Treppass
Aaron Lampros
30 changes: 30 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,42 @@ module.exports = function(grunt) {
passphrase: '',
}
},
custom_base_with_options: {
options: {
base: {
path: 'test',
options: {
index: 'fixtures/hello.txt'
}
},
port: 8014
},
},
multiple_base: {
options: {
base: ['test', 'docs'],
port: 8004,
},
},
multiple_base_with_options: {
options: {
base: [
{
path: 'test',
options: {
index: 'fixtures/hello.txt'
}
},
{
path: 'docs',
options: {
maxAge: 300000 //5min
}
}
],
port: 8015
},
},
multiple_base_directory: {
options: {
base: ['test', 'docs'],
Expand Down
37 changes: 33 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,15 @@ The hostname the webserver will use.
Setting it to `'*'` will make the server accessible from anywhere.

#### base
Type: `String` or `Array`
Type: `String` or `Array` or `Object`
Default: `'.'`

The base (or root) directory from which files will be served. Defaults to the project Gruntfile's directory.
| Type | Result | Example |
| --- | :--- | --- |
| *String* | The base (or root) directory from which files will be served. Defaults to the project Gruntfile's directory. | `'public'` |
| *Array* | Array of `String` (or `Object`) bases to serve multiple directories. The last base given will be the [directory][] to become browse-able. | `['public','www-root']` |
| *Object* | Map containing `path` and `options` keys. `options` are passed on to the [connect.static](http://www.senchalabs.org/connect/static.html) module. | `{ path: 'public', options: { maxAge: 1000*60*5 } }` |

Can be an array of bases to serve multiple directories. The last base given will be the directory to become browse-able.

#### directory
Type: `String`
Expand Down Expand Up @@ -234,6 +237,32 @@ grunt.initConfig({
});
```

#### Connect.static Options
You can specify options to be passed to each instance of the [connect.static](http://www.senchalabs.org/connect/static.html) module:

```js
grunt.initConfig({
connect: {
server: {
options: {
port: 8000,
base: {
path: 'www-root',
options: {
index: 'somedoc.html',
maxAge: 300000
}
}
}
}
}
});
```



####

#### Roll Your Own
Like the [Basic Use](#basic-use) example, this example will start a static web server at `http://localhost:9001/`, with its base path set to the `www-root` directory relative to the gruntfile. Unlike the other example, this is done by creating a brand new task. in fact, this plugin isn't even installed!

Expand Down Expand Up @@ -352,4 +381,4 @@ grunt.registerTask('jasmine-server', 'start web server for jasmine tests in brow

Task submitted by ["Cowboy" Ben Alman](http://benalman.com)

*This file was generated on Mon Jun 09 2014 14:17:55.*
*This file was generated on Mon Jun 30 2014 19:50:00.*
26 changes: 26 additions & 0 deletions docs/connect-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,32 @@ grunt.initConfig({
});
```

## Connect.static Options
You can specify options to be passed to each instance of the [connect.static](http://www.senchalabs.org/connect/static.html) module:

```js
grunt.initConfig({
connect: {
server: {
options: {
port: 8000,
base: {
path: 'www-root',
options: {
index: 'somedoc.html',
maxAge: 300000
}
}
}
}
}
});
```



##

## Roll Your Own
Like the [Basic Use](#basic-use) example, this example will start a static web server at `http://localhost:9001/`, with its base path set to the `www-root` directory relative to the gruntfile. Unlike the other example, this is done by creating a brand new task. in fact, this plugin isn't even installed!

Expand Down
9 changes: 6 additions & 3 deletions docs/connect-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ The hostname the webserver will use.
Setting it to `'*'` will make the server accessible from anywhere.

## base
Type: `String` or `Array`
Type: `String` or `Array` or `Object`
Default: `'.'`

The base (or root) directory from which files will be served. Defaults to the project Gruntfile's directory.
| Type | Result | Example |
| --- | :--- | --- |
| *String* | The base (or root) directory from which files will be served. Defaults to the project Gruntfile's directory. | `'public'` |
| *Array* | Array of `String` (or `Object`) bases to serve multiple directories. The last base given will be the [directory][] to become browse-able. | `['public','www-root']` |
| *Object* | Map containing `path` and `options` keys. `options` are passed on to the [connect.static](http://www.senchalabs.org/connect/static.html) module. | `{ path: 'public', options: { maxAge: 1000*60*5 } }` |

Can be an array of bases to serve multiple directories. The last base given will be the directory to become browse-able.

## directory
Type: `String`
Expand Down
18 changes: 15 additions & 3 deletions tasks/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@ module.exports = function(grunt) {
if (!Array.isArray(options.base)) {
options.base = [options.base];
}
//Options for connect.static module. See http://www.senchalabs.org/connect/static.html
var defaultStaticOptions = {};
var directory = options.directory || options.base[options.base.length - 1];
options.base.forEach(function(base) {
// Serve static files.
middlewares.push(connect.static(base));
var path = base.path || base;
var staticOptions = base.options || defaultStaticOptions;
middlewares.push(connect.static(path, staticOptions));
});
// Make directory browse-able.
middlewares.push(connect.directory(directory));
middlewares.push(connect.directory(directory.path || directory));
return middlewares;
};

Expand Down Expand Up @@ -61,10 +65,18 @@ module.exports = function(grunt) {
// Connect requires the base path to be absolute.
if (Array.isArray(options.base)) {
options.base = options.base.map(function(base) {
if(base.path) {
base.path = path.resolve(base.path);
return base;
}
return path.resolve(base);
});
} else {
options.base = path.resolve(options.base);
if(options.base.path) {
options.base.path = path.resolve(options.base.path);
} else {
options.base = path.resolve(options.base);
}
}

// Connect will listen to all interfaces if hostname is null.
Expand Down
39 changes: 38 additions & 1 deletion test/connect_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,23 @@ exports.connect = {
test.done();
});
},
multiple_base: function(test) {
custom_base_with_options: function(test) {
test.expect(2);
get({
hostname: 'localhost',
port: 8014,
path: '/',
headers: {
accept: 'text/plain',
},
}, function(res, body) {
test.equal(res.statusCode, 200, 'should return 200');
test.equal(body, 'Hello world', 'should return static page');
test.done();
});
},
multiple_base: function(test) {
test.expect(4);
get({
hostname: 'localhost',
port: 8004,
Expand All @@ -100,7 +115,29 @@ exports.connect = {
},
}, function(res, body) {
test.equal(res.statusCode, 200, 'should return 200');
test.equal(res.headers['content-type'],'text/plain; charset=UTF-8', 'should return plaintext content type');
get('http://localhost:8004/connect-examples.md', function(res, body) {
test.equal(res.headers['content-type'],'text/x-markdown; charset=UTF-8', 'should return markdown content type');
test.equal(res.statusCode, 200, 'should return 200');
test.done();
});
});
},
multiple_base_with_options: function(test) {
test.expect(5);
get({
hostname: 'localhost',
port: 8015,
path: '/',
headers: {
accept: 'text/plain',
},
}, function(res, body) {
test.equal(res.statusCode, 200, 'should return 200');
test.equal(body, 'Hello world', 'should return static page');
test.ok(res.headers['cache-control'].indexOf('max-age=0') > 0);
get('http://localhost:8015/connect-examples.md', function(res, body) {
test.ok(res.headers['cache-control'].indexOf('max-age=300') > 0);
test.equal(res.statusCode, 200, 'should return 200');
test.done();
});
Expand Down

0 comments on commit 9dbea9f

Please sign in to comment.