Let’s talk about frameworks. Why do developers use them and which are the most popular ones?
To start, let’s answer the basic question of what is a web framework?
Wikipedia defines it as a software framework that is designed to support the development of web applications including web services, web resources, and web APIs. Web frameworks provide a standard way to build and deploy web applications. They also aim to automate the overhead associated with common activities performed in web development. For example, many web frameworks provide libraries for database access, templating frameworks, and session management, and they often promote code reuse. Although they often target the development of dynamic web sites, they are also applicable to static websites.
So, how do frameworks work?
What Frameworks do for developers is to make our lives easier by offering a structure for application development. They automate the implementation of common solutions, which allows us to cut development time and focus on application logic instead of routine elements.
To understand this we need to talk about web servers, protocols, and specifically HTTP protocol.
- The web server is the application responsible for sending HTML to browsers.
- The protocol is the way the request is being sent and there are several different types like SMTP for emails, HTTPS for secure requests, and FTP for file transfers.
3) HTTP protocol defines a set of request methods to indicate the desired action to be performed for a given resource, The HTTP protocol is based on a request-response model. The client (browser) requests data from a web application (server) that resides on a physical machine.
What are these request methods?
Every message in the HTTP protocol has an associated method. Here are some of the methods:
- GET = > The GET method requests a representation of the specified resource. Requests using GET should only retrieve data.
- HEAD => The HEAD method asks for a response identical to that of a GET request, but without the response body.
- POST => The POST method is used to submit an entity to the specified resource, often causing a change in state or side effects on the server.
- PUT => The PUT method replaces all current representations of the target resource with the request payload.
- DELETE => The DELETE method deletes the specified resource.
- CONNECT => The CONNECT method establishes a tunnel to the server identified by the target resource.
- OPTIONS => The OPTIONS method is used to describe the communication options for the target resource.
- TRACE => The TRACE method performs a message loop-back test along the path to the target resource.
- PATCH => The PATCH method is used to apply partial modifications to a resource.
You might be wondering: what is that cat in the above image? That is called an HTTP Response (status code). The web servers send one with each response to indicate what happened as a result of a given request.
The image above is just an HTTP request-response that we can get through the HTTP method GET, we can keep writing request-responses(GET) and get far but it gets more complicated when we’re building web applications.
How do we map a requested URL to the code that is meant to handle it? We use Web Frameworks. Websites use databases to store information both to be shared with users and about users. Web frameworks provide a database layer that abstracts database read, write, query, and delete operations. This abstraction layer is referred to as an Object-Relational Mapper (ORM).
Using an ORM has two benefits:
- Replacing the underlying database without needing to change the code that uses it; this allows developers to optimize the characteristics of different databases based on their usage.
- It allows basic validation of data, which can be implemented within the framework. This makes it easier and safer to check that data is stored in the correct type of database field, has the correct format (e.g. an email address), and isn’t malicious in any way.
Now let’s dive into the architecture of frameworks.
MVC (Model View Controller)
Most web frameworks are based on MVC architecture. MVC separates the data model with business rules from the user interface. It has three main parts: Model, View, & Controller.
- The Model is responsible for database access and executing queries. It represents the data of the application.
- The Controller’s task is to handle the business logic of the application, it should use the Model to access the database along with user-input to construct the information or execute the action the user requests.
- The View is the part visible to the user. Data generated by the Controller is displayed via the View. Conversely, any input that the user enters in the View is passed on to the Controller.
There are so many frameworks out there, here are a few that are MVC:
- Sinatra written in Ruby
- Ruby on Rails written in Ruby
- Flask written in Python
- Django written in Python
- Spring Framework written in Java
The purpose of web frameworks is to hide the boilerplate and infrastructural code related to handling HTTP requests and responses. They provide tools and libraries to simplify common web development operations.
There is others type of frameworks that I didn’t touch on in this article such as RMR(Resource-Method- Representation). Each developer has ones they prefer and I encourage you to explore and see what works best for you.