I will also cover some design, but it should be taken with a grain of salt. I am not a proficient designer, but I often find myself doing design work. I will focus mostly on the technical aspects of design, an area where I have made a lot of mistakes in the past.
I'm not going to talk all that much about how to use specific technologies. Instead I'm going to talk about why and where to use specific technologies, and discuss concepts. There are heaps of articles available that explain in excruciatingly pointless detail how to use specific tech.
I'm aiming these posts at a very specific set. These people are at where I was before I got started building for web. They:
- Already know how to program and understand object-oriented programming.
- Are familiar with Python.
- Are familiar with HTML and CSS.
- Have not built a web application before, but have set up / worked on sites in the past.
As far as I can tell, Rails gives Ruby developers a unified way to get into web tech, so they are less likely to find something like this useful. I am not the most devout web developer. Indeed, my writing this is borne of my frustrations with the activity. Those more entrenched in the job may provide better overviews.
HTTP
The first bad move I made was not bothering to understand HTTP. It's not something that web developers can take for granted. HTTP is not only a technical detail, it informs the so-called architecture of programs that use it. You don't have to know what I'm about to summarise to make a web application, but your life will be easier and your code will be less stupid if you do.
The unavoidable detail is that HTTP splits communication into requests and responses to those requests. The client sends requests to the server, and the server processes those requests and responds. Server-side programming consists largely of processing requests and rendering responses to be sent. Reading RFC2616, the HTTP 1.1 spec, would probably be overkill. Some people recommend it, but I haven't found the need.
An HTTP request consists of three parts:
- A request line, which in turn consists of:
- The URI (address) of a "resource", e.g. a page, a file, a blog post, a tweet.
- The "method" - what to do to the resource. Create, update, delete, etc.
- The HTTP version (the browser or lower-level server-side software takes care of this for you - it's going to be version 1.1.)
- Some headers specifying additional information about the nature of the request, rules, etc. I haven't been able to find a clear explanation of what the intent of headers is in the RFC. In practice, setting them often allows additional control over requests, which may fix problems or make your life easier. They are used by lower-level software to indicate how things are encoded and the such.
- A body. This part is optional, but is used to contain things like form data.
The most common request methods are:
- GET - get whatever information is identified by the URI.
- POST - add the entity represented in the request body as a new "subordinate" of the resource at the URI. e.g. create a new product in a catalogue.
- PUT - store the entity represented in the request body at the URI. If there's already something at the URI, update it.
- DELETE - delete the thing at the URI.
- HEAD - same as GET but the server must not return a body. Basically returns the headers without transferring the whole entity.
A HTTP response is similar to a HTTP request except that instead of a request line, it has a status line. A status line consists of the HTTP version, a status code summarising how the request went, and a reason phrase explaining the status code.
Status codes (with their default reason phrases) you have probably seen before are:
- 404 Not Found
- 500 Internal Server Error
- 403 Forbidden
- 200 OK
- 201 Created
- 301 Moved Permanently
- 503 Service Unavailable
The body of the response will often (but not always) contain a web page in HTML. The Content-Type header tells the client (probably your browser) what sort of data is in the response body.
The most common way to send a request is using a HTML form. Here is a form that requests the greatest common divisor of two integer numbers:
<form id="gcd-form" method="get" action="/gcd/">
<label>A</label>
<input type="text" name="first_int"/>
<label>B</label>
<input type="text" name="second_int"/>
<input type="submit" value="Calculate GCD">
</form>
This will print out a form that looks like this:
In theory, we could use it to send a request to a program that will calculate the GCD of the two integers. Based on what I've talked about at so far, we can guess that the integers would appear in the body of the request, the "action" parameter will tell us the URI of the request, and the method parameter (surprisingly) tells us the method of the request.
Congratulations, you now know things about HTTP that most beginning web developers don't bother to find out. Hopefully it makes your life easier in future. Next time I will go over the application of what we learned to the simple GCD example, as well as looking at REST, an "architectural" idea that builds on HTTP.
If you want to fact-check or clarify my ramblings on HTTP basics, the RFC can be found at http://tools.ietf.org/html/rfc2616.