Motivation
If you are like I was, a former CS major whose programming experience consisted of writing command-line programs using C++, the world of web applications might seem mysterious and foreign.
You know how computers work i.e. you know processor architecture, you know the basics of operating systems, compilers, networking protocols… but this web stuff is something else. And it’s hard to get started – for one because there’s all this terminology, but even if you learn the terminology it feels like there’s all this magic going on.
This blog post is for people who were in the position I was in 6 months ago. It is not a tutorial – it is meant to be read before your tutorials so that things make sense.
Web Application – Basic Flow
First let’s break down the basic structure of a web application.
Ok, simple enough.
Web Frameworks
So what do web frameworks do for you? Keeping in mind the steps above, the framework should provide:
Another useful feature is an authentication framework, so you can mark certain endpoints as "protected" i.e. requiring a logged-in user. Of course to do this you need the concept of a user, so let's not get into that.
If you are like I was, a former CS major whose programming experience consisted of writing command-line programs using C++, the world of web applications might seem mysterious and foreign.
You know how computers work i.e. you know processor architecture, you know the basics of operating systems, compilers, networking protocols… but this web stuff is something else. And it’s hard to get started – for one because there’s all this terminology, but even if you learn the terminology it feels like there’s all this magic going on.
This blog post is for people who were in the position I was in 6 months ago. It is not a tutorial – it is meant to be read before your tutorials so that things make sense.
Web Application – Basic Flow
First let’s break down the basic structure of a web application.
- An application will expose multiple endpoints, which usually correspond to different parts of the application but don’t necessarily need to. An endpoint is represented by a URL, like www.myapp.com/register or www.myapp.com/logout.
- Clients make requests to these endpoints. These use the HTTP protocol, which I won’t get into other than to say that a request is made up of:
- Headers, some of which are used during transit and delivery, but you can add your own
- The desired endpoint
- An HTTP verb, like GET or POST
- GET requests will often have a query string attached, like /getInfo?name=Rohit&age=36
- POST requests will usually have a body, which could be XML, JSON or something called “form-data”
- When the application starts up, it registers its endpoints with the web server (a separate process running on that machine). Registration means providing callbacks for these endpoints.
- When a request comes in, the web server matches the URL against the endpoints it has registered, to see if there is a listener available. If not, it returns an error message (following what the HTTP protocol defines, hopefully). Otherwise it passes it on to the registered handler.
- The handler does what it needs to, and then sends an HTML response back to the web server
- The web server sends this response back to the client
Ok, simple enough.
Web Frameworks
So what do web frameworks do for you? Keeping in mind the steps above, the framework should provide:
- A simple endpoint & handler registration mechanism
- Automatic parsing of the request, putting the information into some simple data structures
- Simple ways for handlers to redirect a request (possible modified) to other endpoints
Another useful feature is an authentication framework, so you can mark certain endpoints as "protected" i.e. requiring a logged-in user. Of course to do this you need the concept of a user, so let's not get into that.
But an even more useful feature is layout templating. Think of your final page as being data placed in a layout container. The layout defines the colors, the text areas, navigation controls, where the scroll bars are, which buttons do what etc. Some of these don't change depending on the data being presented, and some do. The template will define the data-independent portions and indicate where the data is meant to go. It might be as simple as
<div id="userName" class="userNameDisp">
{{userNameVar}}
</div>
The framework will read this HTML template, figure out that userNameVar is a variable whose value is required, and get that variable's value from the interpolation context. You can imagine this working like this:
getPage (templateName, dataContext):
response = loadTemplateAsString (templateName)
for every {{varName}} in the response string
look up varName in the dataContext
replace {{varName}} by this value in the response string
return response
The two web frameworks which I know of which work this way are Django, written in Python, and Ruby on Rails, which is written in Ruby.
A different strategy
Nowadays everyone loves JavaScript, supposedly because browser support is so much better than it was before (I don't know how it used to be, this is just what I hear). In fact browsers are so good at running JavaScript now that applications are being written so that a large part just sits in the browser!
One of these frameworks is called AngularJS, and I've actually written a small application using it. But I don't fully know how it works yet. Here's what I do know/ believe at this point:
- Request to the web application loads a whole bunch of JavaScript code. This contains all the endpoint definitions and associated handlers.
- As the user interacts with the application, requests will be triggered to different endpoints. These will, as far as possible, be trapped by the client-side application and NOT be translated into fresh requests to the server (since the application logic has already been loaded into the browser)
- Of course certain requests do need to go to the server, like requests for data. The server will now just send back JSON or XML.
- Layout templates will also be sent on request, though a framework might also provide a mechanism for pre-requesting and caching them locally. One would hope that a layout template is not loaded more than once though.
Now the server sends only three types of responses: the initial application code, layout templates, and raw data. Less network traffic, but more work for the browser.
I hope this helped. If you want to learn more about these JavaScript frameworks, you can Google
- AngularJS for the client side (and yes there are others)
- Express for the server (again, there are others)
- and for solutions which combine both the client and server - Meteor and Sails!