In these series of posts we have been working with Firebase as the database and application server. While Firebase does a lot of the application server functions well, e.g., providing a CRUD API, authentication, authorization, and even validation, at this point it cannot perform any additional operations, e.g., get or send data through a third-party API. It is my understanding this is what they are working on through their acquisition by Google.
note: Referential integrity within Firebase can be maintained using a combination of validation rules and use of the onDisconnect feature (more on that later).
As this is a serious limitation (really do not want the client to have to do all the work; esp. if the integrity of the data is at stake), one has to consider alternative solutions to perform these sorts of operations.
I have explored two modern server frameworks (Ruby on Rails and Node.js / Express) and here are my observations:
Ruby on Rails
+ Popular with Developers
+ Popular with Clients
+ Opinionated; i.e., with all the generators and such there is a prescribed way of doing things.
- Ruby (I already know JavaScript from the client; context switching to Ruby is a pain).
- Opinionated; i.e., there is a quite a bit to learn to get started.
- Generators; I find code that generates code distasteful.
- Overkill: Much of the Ruby on Rails examples assume that one is writing a server-side application that needs to handle things like sessions and HTML generation.
Node.js / Express
+ Popular with Developers
+ Not Opinionated (Flexible); i.e., easy to get started.
+ JavaScript (I already now JavaScript from the client).
- Unknown to Clients
- Not Opinionated (Flexible): i.e., with many different ways of doing things, harder to support others code.
My Opinion on The Future
With the explosive growth in JavaScript development, one can only assume that Node.js / Express will eventually dominate <
http://nodejs.org/industry/>.
Example of Building RESTful API on Ruby on Rails
I spent a better part of a day trying to find a good tutorial on this; never found one. What I ended up doing is going through the main Ruby on Rail tutorial <
http://guides.rubyonrails.org/getting_started.html> (focused on server side HTML generation) and then piecemeal getting to a simplified RESTful API from a myriad of partial tutorials (or written poorly enough that I did not get it).
The key pieces were:
All together the key controller code (simple Create and Read example only) is:
note: In my sample code below, I include all the parenthesis as I find it difficult to understand without it. Apparently, Ruby has a myriad of ways to shortcut code (yuck).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class ArticlesController < ApplicationController
def index
articles = Article.all()
render({json: articles.as_json({only: [ :id , :title ]})})
end
def create
params().require( :title )
article = Article.create(params().permit( :title ))
render({json: article.as_json({only: [ :id , :title ]})})
end
end
|
Example of Building RESTful API on Node.js / Express
While the following example is new to me, it walks one through from ground-zero to a RESTful API in a single HTML page. It does use the express generator (something that I actually never knew about and might use myself despite by avoidance of such things).
http://adrianmejia.com/blog/2014/10/01/creating-a-restful-api-tutorial-with-nodejs-and-mongodb/