Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grouping routes for similar functions into seperate files #21

Closed
imolorhe opened this issue Jan 11, 2017 · 4 comments
Closed

Grouping routes for similar functions into seperate files #21

imolorhe opened this issue Jan 11, 2017 · 4 comments

Comments

@imolorhe
Copy link

Hello,

Is it possible to group routes into seperate files the same way you would do it in express?

In express, you could just do:

app.use('/animals', require('./animals'))

and inside animal.js, you could have:

var express = require('express')
  , router = express.Router()

// Domestic animals page
router.get('/domestic', function(req, res) {
  res.send('Cow, Horse, Sheep')
})

// Wild animals page
router.get('/wild', function(req, res) {
  res.send('Wolf, Fox, Eagle')
})

module.exports = router

Such that all the routes are preceded with /animals. Can this be achieved in rill as well?

@DylanPiercey
Copy link
Member

This is totally possible and works much the same as express.

The big difference is that you can nest subrouters into any rill method (at, get, put, post, etc) and that you have to use a catch all path like '/animals/*'.

Your above example would look like this:

index.js

app.at('/animals/*', require('./animals'))

animals.js

var rill = require('rill')
  , router = rill()

// Domestic animals page
router.get('/domestic', function(rctx) {
  ctx.res.body = 'Cow, Horse, Sheep'
})

// Wild animals page
router.get('/wild', function(ctx) {
  ctx.res.body = 'Wolf, Fox, Eagle'
})

module.exports = router

Feel free to ask more questions if this unclear.
Thanks for your question!

@imolorhe
Copy link
Author

@DylanPiercey So they can't be exactly the same then.

@DylanPiercey
Copy link
Member

@imolorhe what do you mean? I never claimed for it to be exactly the same as express 😜. Rill itself more similar to koa than express but ultimately it's different than both because it is optimized for both the browser and the server.

@imolorhe
Copy link
Author

@DylanPiercey Okay. How about passing multiple middlewares to app.get() or app.at()?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants