From eae4382e00686ec982914fee306e87654345e507 Mon Sep 17 00:00:00 2001 From: Vinicius Teixeira Date: Mon, 18 Jan 2016 22:37:06 -0200 Subject: [PATCH 1/2] updating readme to include instructions about connect 3.x API changes --- README.md | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 73d6812..1c4cf65 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ grunt.initConfig({ ``` #### Adding the middleware -##### With Livereload +##### With Livereload and grunt-contrib-connect <= 0.10 Add the middleware call from the connect option middleware hook ```js @@ -82,6 +82,52 @@ Add the middleware call from the connect option middleware hook } ``` +##### With Livereload and grunt-contrib-connect >= 0.11 + +grunt-contrib-connect 0.11 onwards is using connect 3.x under the hood. This means +that `connect.static` and `connect.index` are no longer available. So, if you run +into an error like `connect.static is not a function`, you might be using connect 3.x. + +To fix this problem, load [`serve-static`](https://www.npmjs.com/package/serve-static) and/or +[`serve-index`](https://www.npmjs.com/package/serve-index) an configure the connect task as +follows. + +```js + + var serveStatic = require('serve-static'); + var serveIndex = require('serve-index'); + + grunt.initConfig({ + //... + connect: { + livereload: { + options: { + middleware: function (connect, options) { + if (!Array.isArray(options.base)) { + options.base = [options.base]; + } + + // Setup the proxy + var middlewares = [require('grunt-connect-proxy/lib/utils').proxyRequest]; + + // Serve static files. + options.base.forEach(function(base) { + middlewares.push(serveStatic(base)); + }); + + // Make directory browse-able. + var directory = options.directory || options.base[options.base.length - 1]; + middlewares.push(serveIndex(directory)); + + return middlewares; + } + } + } + } + }); +``` + + ##### Without Livereload It is possible to add the proxy middleware without Livereload as follows: From f3bae6387ae68246c0a90e933848be7459fa9dff Mon Sep 17 00:00:00 2001 From: Vinicius Teixeira Date: Mon, 18 Jan 2016 22:47:21 -0200 Subject: [PATCH 2/2] fixing typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1c4cf65..183302a 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ that `connect.static` and `connect.index` are no longer available. So, if you ru into an error like `connect.static is not a function`, you might be using connect 3.x. To fix this problem, load [`serve-static`](https://www.npmjs.com/package/serve-static) and/or -[`serve-index`](https://www.npmjs.com/package/serve-index) an configure the connect task as +[`serve-index`](https://www.npmjs.com/package/serve-index) and configure the connect task as follows. ```js