Is there a reason you didn't do this?
Is there a reason you didn't do this?
Your main Web server could just serve the initial page / application / whatever, and then the application lives on the client, fetching data from the API and formatting it as needed. You end up with proper MVC in the browser.
That's what I did for a small side-project of mine last week and it worked very well. I detailed how I built it here: https://plus.google.com/118077068834135870002/posts/62agb7qp...
$user = $api->GET('/accounts/the_user');
which would process the API call without actually making a separate HTTP request. Would this accomplish it?However, there are two reasons why you would access the API with server side code in a web client (as opposed to a mobile one).
[1] You can provide a Google friendly, static (no javascript) version of the site and all content - as opposed to a completely invisible site from an SEO perspective.
[2] You can store local versions of all data that's returned from API calls.
This is useful in a couple of ways.
[a] It means that you need only check the API for a 'last modified' date and, if it's older than the current date, use the locally cached API call results first (if they exist). This makes the client extremely fast, since you're reading flat files locally.
[b] If the API is inaccessible for some reason, the client can default to using the locally cached copies of previous API call results
I built this site (backend, not the HTML/CSS/JS) the same way a while back and have been pleasantly surprised with the results - http://jacksonteece.com/
When you have different templates based on user rights, you should probably make different packed template files and serve them only to users with the proper cookie / session rights.
I'd serve an admin page / app at a specific URL and then it would reference the proper template JS file (or whatever you're using).
Either way I wouldn't worry about the extra few bytes served for rarely used templates, just make sure the template file is properly cached. Or serve it as a different file and load it asynchronously when needed.
I found there were very few good alternatives for using Jade template client-side. The only one that seems promising is JadeVu (https://github.com/LearnBoost/jadevu/) but the approach is very different.