From b0735dc7d0a649d62e815c700e8444b064647824 Mon Sep 17 00:00:00 2001 From: Walter Barbagallo Date: Fri, 8 Apr 2016 18:08:20 +0200 Subject: [PATCH 1/3] add root method --- lib/router.js | 7 +++++++ test/guidanceTest.js | 16 ++++++++++++++++ test/utils/controllers/welcome.js | 3 +++ 3 files changed, 26 insertions(+) diff --git a/lib/router.js b/lib/router.js index 155af12..ada8707 100644 --- a/lib/router.js +++ b/lib/router.js @@ -119,6 +119,13 @@ Router.prototype._parseControllerActionNotation = function parseControllerAction }; +Router.prototype.root = function root(opts) { + this.layers.push(this._parseRoute({method: 'get', path: '/', opts })); + + return this; +}; + + Router.prototype.resources = function(resource, fn) { if (_.isArray(resource)) { resource.forEach((resource) => { diff --git a/test/guidanceTest.js b/test/guidanceTest.js index 7cb26e6..e799e24 100644 --- a/test/guidanceTest.js +++ b/test/guidanceTest.js @@ -93,6 +93,22 @@ describe('guidance', function() { .end(done) ; }); + + + it('use root method', function(done) { + + let routes = function(router) { + router.root({ to: 'welcome#index'}); + }; + + app.use(guidance.initialize(routes, { controllersDir })); + + request(app) + .get('/') + .expect(200) + .end(done) + ; + }); }); context('resource', function() { diff --git a/test/utils/controllers/welcome.js b/test/utils/controllers/welcome.js index 472b297..19dc985 100644 --- a/test/utils/controllers/welcome.js +++ b/test/utils/controllers/welcome.js @@ -2,6 +2,9 @@ module.exports = { index: function(req, res) { res.json({ message: 'index action' }); }, + about: function(req, res) { + res.json({ message: 'about action' }); + }, homepage: function(req, res) { res.json({ helpers: { From eaf5d1110be350b981d1cd9f4cf16f1d178039b9 Mon Sep 17 00:00:00 2001 From: Walter Barbagallo Date: Fri, 8 Apr 2016 18:15:40 +0200 Subject: [PATCH 2/3] add shorthand root method --- lib/router.js | 1 + test/guidanceTest.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/router.js b/lib/router.js index ada8707..8d79116 100644 --- a/lib/router.js +++ b/lib/router.js @@ -120,6 +120,7 @@ Router.prototype._parseControllerActionNotation = function parseControllerAction Router.prototype.root = function root(opts) { + if (_.isString(opts)) opts = { to: opts }; this.layers.push(this._parseRoute({method: 'get', path: '/', opts })); return this; diff --git a/test/guidanceTest.js b/test/guidanceTest.js index e799e24..752fc51 100644 --- a/test/guidanceTest.js +++ b/test/guidanceTest.js @@ -109,6 +109,22 @@ describe('guidance', function() { .end(done) ; }); + + + it('use root method shorthand', function(done) { + + let routes = function(router) { + router.root('welcome#index'); + }; + + app.use(guidance.initialize(routes, { controllersDir })); + + request(app) + .get('/') + .expect(200) + .end(done) + ; + }); }); context('resource', function() { From 0d6513c3e180149da9a0e12e2315c8149d550e6b Mon Sep 17 00:00:00 2001 From: Walter Barbagallo Date: Fri, 8 Apr 2016 18:22:12 +0200 Subject: [PATCH 3/3] add documentation --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 827b051..29b60e9 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,16 @@ When the express app receive a `GET /home` request, the index action handler of Any express supported method (router.METHOD) can be used. +#### Root + +You can specify how to route `GET /` with the root method: + +```javascript +router.root({ to: 'welcome#index' }) +router.root('welcome#index') // shortcut for the above +``` + + #### Named parameters Named parameters are also supported: