mansona / express-autoroute

Helper module to automatically load Express routes from a structured routes/ folder
MIT License
35 stars 10 forks source link

Feature request #9

Closed Dexus closed 10 years ago

Dexus commented 10 years ago

Object with all generated links. A function to generate links with params etc. available over the locals for using in templates.

mansona commented 10 years ago

Hi @Dexus, thanks for the suggestion.

I'm sorry but I don't understand what exactly you are asking for, if you could provide a code sample for the API you would like we can discuss it here.

Dexus commented 10 years ago

Hi @mansona ,

let us think we have a route:

ORGINAL:
app.get('/products/:slugname/', function_to_get_slugname);

EXAMPLE WITH 'SHORTNAME':
app.get('/products/:slugname/','shortname_for_uri', function_to_get_slugname);

now i would like to use

app.locals.genUri('shortname_for_uri', {'params':{':slugname':'name-of-the-site'},'qs':{'get_arg':123}});

OUTPUT:
/products/name-of-the-site/?get_arg=123

app.locals.genUri('shortname_for_uri', {'params':{':slugname':'name-of-the-site'}});

OUTPUT:
/products/name-of-the-site/

I hope you understand what i mean. My English is a little rusty.

EDIT: Like this PHP Versions: https://github.com/dannyvankooten/PHP-Router or https://github.com/dannyvankooten/AltoRouter

mansona commented 10 years ago

Well my first question is.. why would you want to do that? why couldn't you just put the products endpoint in the 'name-of-the-site' folder

// filename /routes/name-of-site/products.js

module.exports.autoroute = {
    get: {
        '/products/:slugname' : function_to_get_slugname,
    }
};

This would produce a /name-of-the-site/products/slug_name endpoint which is similar to what you want.

The other thing to remember is that the JSON object that you set module.exports to be can be generated by a custom function.

var autorouteObj = {
    get: {}
};

[0,1,2,3,4,5].forEach(function(item){
    autorouteObj.get['/product/' + item] = someFunction;
}

module.exports.autoroute = autorouteObj;

That would produce custom endpoints /product/1 , /product/2 , /product/3 , /product/4 .

do you understand my example? I think with this you should be able to produce what you want, but again I have to wonder if what you want is the "right" way to do this.... there is probably an easier way ;)

Dexus commented 10 years ago

Okay, that would ne possibility, but will if I have a website created in the new products (sites), some of which are drawn from a database. I want this site new link without which I would like every one is mature in the source code. It is easiest for me if I have a URI generator, from which I can generate with "short keys" and a URI parameters for the source code (directly in the template eg ejs).

Our Website have over 60 undersites per category and up to 5 undersites per product. So it is not easy, that all expand by hand.

mansona commented 10 years ago

So the idea of "undersites" or what i would call subdomains or vhosts is not within the scope of express-autoroute, this is both because it is not commonly done at the app level and because it is not something that is handled by express.

I would suggest that you have a look at these: stack overflow answers here and here

I would also warn against doing something like this because if the functionality /products/site1/?get_arg=123 and /products/site2/?get_arg=123 are the same then why can't they just be /products/?get_arg=123 or /products/?get_arg=123&site=site1 or something more like this

Dexus commented 10 years ago

the querystring is only an example for affilinate links or other informations that only needes for temp informations.

The important thing is the "slugname" that call the data out of the DB. Okay undersites in my case are dirs.

/whmcs-modules/ <-- Main Category
/whmcs-modules/:slugnameOfProduct/ <- Main Product
/whmcs-modules/:slugnameOfProduct/:slugnameOfDifferentDetailpages/ <-- up to 5 Detail Pages

/other-modules/
/other-modules/:slugnameOfProduct/
/other-modules/:slugnameOfProduct/:slugnameOfDifferentDetailpages/

That all handled by my Function, currently i hardcode the links... but i search to use for each link with in the autoroutes a shortname so that i can generate the links easy. Its like a CMS, there you woun't like to hardcode links ;)


EDIT on 03:08 UTC+1:

Found a solution: https://github.com/web-napopa/node-reversable-router (use npm install and after it update from git to fix bugs)

mansona commented 10 years ago

Cool, I'm glad you found something that helps :+1: