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: diff --git a/lib/router.js b/lib/router.js index 155af12..8d79116 100644 --- a/lib/router.js +++ b/lib/router.js @@ -119,6 +119,14 @@ 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; +}; + + Router.prototype.resources = function(resource, fn) { if (_.isArray(resource)) { resource.forEach((resource) => { diff --git a/test/guidanceTest.js b/test/guidanceTest.js index 7cb26e6..752fc51 100644 --- a/test/guidanceTest.js +++ b/test/guidanceTest.js @@ -93,6 +93,38 @@ 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) + ; + }); + + + 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() { 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: {