Skip to content

Commit

Permalink
Merge branch 'feature/root-method' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Walter Barbagallo committed Apr 8, 2016
2 parents 5b3b014 + 0d6513c commit 148a471
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
32 changes: 32 additions & 0 deletions test/guidanceTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
3 changes: 3 additions & 0 deletions test/utils/controllers/welcome.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 148a471

Please sign in to comment.