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

Add grouping route #32

Closed
oscarr-reyes opened this issue Feb 22, 2016 · 14 comments
Closed

Add grouping route #32

oscarr-reyes opened this issue Feb 22, 2016 · 14 comments
Assignees
Milestone

Comments

@oscarr-reyes
Copy link

Could be a nice idea.. but what if there was a function that groups all methods in a single route?

for example:

app.group('/books', function(method){
    method.get(function($){

    });

    method.post(function($){

    });

    method.put(function($){

    });

    method.delete(function($){

    });

    // Other methods too..
});
@adamhalasz adamhalasz self-assigned this Feb 22, 2016
@adamhalasz
Copy link
Owner

Hey @Nosthertus pretty good idea!

@oscarr-reyes
Copy link
Author

great.. i came up with this when i was developing diet-raml-generator

@tobbbles
Copy link

Syntactically, I think an event emitter would be a better option, example:

app.group('/books', function(method){

    method.on("get", function($){

    })
    .on("post", function($){

    })
    .on("put", function($){

    })
    .on("delete", function($){

    })
    // Other methods too..
});

@oscarr-reyes
Copy link
Author

@mnzt that is also a good idea. there could also be a function to catch all methods at once

@frank-dspeed
Copy link
Contributor

At present i group it like this:

I create out of diet.js a server framework to listen for many thousend apps and domains. for my usecase maybe you can use that infos:

i do for the main server a global.server = diet
then i create diffrent controller/app1

then in the main server i do server.controller('app1')

so app1 needs top be a js file that defines a normal dietjs app and use a costum name for the server object. like app1= require diet ()
now app1 header or other get routes can use app1.controller and server.controller methods to require its files in a consistent way.

so app1 is the complet app group that is complet configured when i now whant to use that for a domain i only need to do in the main server index.js where we started

app1.listen('protocol://rest')

i can repeat that many 1000 times or simply let app1-4 listen for diffrent groups of domains.

then you create middelware functions so you can reuse the methods in all your apps and combine them so to groups.

greetings.

@adamhalasz adamhalasz added this to the Diet v1.0 milestone Jul 23, 2016
@adamhalasz
Copy link
Owner

adamhalasz commented Jul 23, 2016

Hey guys, love this topic. I will add this feature in the upcoming v1.0.

The API will most probably be like this:

var book = app.resource('/book')
    book.post(function($){ ... })
    book.put(function($){ ... })
    book.get(function($){ ... })
    book.delete(function($){ ... })

This will allow chaining the method listeners as well:

app.resource('/book')
   .post(function($){ ... })
   .put(function($){ ... })
   .get(function($){ ... })
   .delete(function($){ ... })

Kind Regards,
Adam

@adamhalasz
Copy link
Owner

@frank-dspeed can you share some code snippets for your implementation?

@oscarr-reyes
Copy link
Author

i suppose resource() would be something new to add to the API, but either way the example looks very nice, and really glad this framework is close to hit 1.0

@frank-dspeed
Copy link
Contributor

Ya need to find the example have at present to much un sorted codes but will ping you back on this and will always watch this repo for current state

@luisvinicius167
Copy link

I changed the source code and I implemented this for me, and it is working very well.

app.group('/api/v1').then( route => {
  route('get', '/home', $ => { $.end(`User Home`) });
  route('post', '/insert', $ =>{ $.end(`Insert an User`) });
});

app.group('/api/v2').then( route => {
  route('get', '/home', $ => { $.end(`User Home v2`) });
  route('get', '/contact', $ => { $.end(`User Contact v2`) });
});

@luisvinicius167
Copy link

I created a grouping route module for Diet.js. For me, it's very useful: https://github.com/luisvinicius167/diet-group-router

adamhalasz added a commit that referenced this issue Mar 2, 2017
- Introducing the app.resource(path) class for grouping methods - inner
functionality inspired by @luisvinicius167 's diet-group-router #32
@adamhalasz
Copy link
Owner

adamhalasz commented Mar 2, 2017

As of 0.13.0 grouping is supported with app.resource(path). It works as described above in my previous comment.

var book = app.resource('/book')
    book.post(function($){ ... })
    book.put(function($){ ... })
    book.get(function($){ ... })
    book.delete(function($){ ... })

You can also chain it as such:

app.resource('/book')
   .post(function($){ ... })
   .put(function($){ ... })
   .get(function($){ ... })
   .delete(function($){ ... })

For reference check out the Resource Example:
https://github.com/adamhalasz/diet/blob/master/examples/resource/index.js

@oscarr-reyes
Copy link
Author

should i send a pull request of this in diet docs? or are you planning to add this in the docs at some time?

@frank-dspeed
Copy link
Contributor

@Noshertus you can open a issue to add that to the docs but i think it has no core priority

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

No branches or pull requests

5 participants