appex icon indicating copy to clipboard operation
appex copied to clipboard

Have multiple handlers for the index route?

Open grofit opened this issue 12 years ago • 0 comments

I am just wondering how is best to go about the following scenario, lets say I want the route http://some-host.com/test to have 4 different handlers based upon the verb being used.

So for example someone goes to http://some-host.com/test as a GET request, and we have the following function in place to catch it:

export module test
{
    export function index(context)
    { return context.response.send(200, "GET Success"); }
}

This works fine and will respond with "GET Success". However let us now assume for the same route I want to treat POST requests differently but I want the route to be the same. I would expect I would need to use attributes to do this.

export module test
{
    attribute('index', { verbs:['GET'] });
    export function get(context)
    { return context.response.send(200, "GET Success"); }

    attribute('index', { verbs:['POST'] });
    export function post(context)
    { return context.response.send(200, "POST Success"); }
}

Now this does not work, and thinks I want test/get to be the route, however I was under the illusion that the index attribute flagged this method as being a handler for an index based request however I assume I am wrong and it just identifies the function name it pertains to and for the route related arguments applied to the attribute.

So basically I am wondering if there is a way to make RESTful style modules where they have GET/POST/PUT/DELETE based index handlers with different function names. I assume that I can use the url re-write attribute arguments but is there a more succinct way of doing it?

grofit avatar Dec 04 '13 16:12 grofit