peerigon / alamid

Framework for RESTful JavaScript web applications that run both on the server- and clientside.
http://www.alamidjs.com
MIT License
23 stars 3 forks source link

Middleware: Request-Params handling #170

Closed meaku closed 11 years ago

meaku commented 11 years ago

Middler offers param parsing, i.e /services/user/:type/:count would extract the params into an object, which is currently not passed on to the alamid-request object.

We should add an params-attribute which will be populated with those params, if defined in middleware.

jhnns commented 11 years ago

Yep. This is a main feature of middler / express.

meaku commented 11 years ago

Param handling works now.

You can call req.getParams to retrieve the params or simply access the req.params attribute.


server.addRoute(["read"], "/services/test/:id*", function(req, res, next) {

    console.log(req.getParams().id);

    //forward to different service
    req.setPath("/services/todo");

    next();

});

There are still many things, that can be improved in the future... It's also important to define all middleware with a slash at the beginning.

meaku commented 11 years ago

It's fine for now, because the API will be stable.

meaku commented 11 years ago

There is a change related to this new feature concerning middleware-definition. I was using the internal sanitized path for route-resolving before and switched to the raw path.

RawPath /services/group/123/members alamid-path -> /services/group/members

alamid expects service-urls to have the following order:

services/ modelURL / modelID / subModelURL / subModelID

The IDs will be removed from the path and stored internally in an IDs object.

We have to use the rawPath for middleware-routing because you might want to define routes, that differ from the pattern above. That's why we need to change the middleware-definition.

Example

Last alamid version

server.addRoute(["create", "update", "destroy"], "/services/group/member*", myMiddleware);

New alamid version

server.addRoute(["create", "update", "destroy"], "/services/group/:groupId/member*", myMiddleware); 

It's maybe slower than before, because middleware will extract the ids as params. But i think it's more readable and easier to customize. But we have to take more care of the raw-path if it's directly passed to middler. We might add some sanitization to the rawPath.

jhnns commented 11 years ago

Yes, it's better, because you don't have to know what alamid does in the background and what alamid expects. It improves readability if the code tells what's going on behind the scenes :wink:

meaku commented 11 years ago

Nice! We'll keep it this way!