rendrjs / rendr

Render your Backbone.js apps on the client and the server, using Node.js.
MIT License
4.09k stars 312 forks source link

Separating a route to only exist on the server #108

Open craigedmunds opened 11 years ago

craigedmunds commented 11 years ago

I'm trying to implement OAuth for an api that requires it, and want to keep the client key and secret only on the server.

It seems pretty straightforward to pepper if(global.isServer) around the routes and templates, but is there a better way already existing?

How should the controller be seperated?

I've started looking into ServerRouter, and this looks promising for the route side of things, but will the templates when rendered clientside detect that the route doesn't exist and so direct it to the server?

spikebrehm commented 11 years ago

So far, there's no way to separate Rendr's routes as being only client-or-server side. I'm not entirely sure what you're trying to accomplish; could you explain more about what these server-only endpoints should do?

Remember that a Rendr app is at the end of the day just an Express app, so you can mount any server routes you want as you would with any Express app, going around Rendr entirely.

craigedmunds commented 11 years ago

I've put my ideas into a fork of your repo here https://github.com/craigedmunds/rendr-app-template

This includes an update to the rendr repo too. There's work todo in both repos before it could be used seriously, but I think it adequately demonstrates what I mean.

What I'm trying to achieve is to have the auth routes, and related code only on the server, and not sent down to the client, as I don't want the keys exposed.

Make sense?

c089 commented 11 years ago

I'm struggling with a very similar use case: For some routes, I want to have code that will only be run on the server. This is code that needs to load a model using an id extracted from the URI by the route to decide wether or not the view will be rendered or an HTTP redirect will occur. The only solution I currently see is to put this code into the controller and wrap it in an if (isServer). However that does not seem clean and in other situations might even expose internals to the visitor.

My first attempt at doing this was to use a middleware, but the problem with that is the fact that I need some data extracted from the path by the routing. So what I would need would be a more flexible route information that is closer to this:

  match('/page/:id/'  { serverHandler: function (), sharedHandler: () });
alexindigo commented 11 years ago

I'm working on allowing Rendr to load controllers conditionally, which would help to reduce initial payload, and for this issue, userland functions can decide what to load on the client.

mmautner commented 10 years ago

It sounds like this was never resolved, and that the best solution is to add another express middleware for these server-only routes.

Examples of such server-only routes might include salting a password/using oauth secret keys before performing user auth operations.