In this recipe, we explained how we can do routing. A URL (for example, http://example.com:8080/some/path?param) has these two main parts:
- The first part locates the server and service, and is composed of a scheme (such as HTTP/HTTPS), a host name (example.com), and a port.
- The second part is composed of a path or routes, and query parameters. (/some/path?param).
The generic schema is explained at https://en.wikipedia.org/wiki/URL.
In a web application, we are interested in the HTTP/HTTPS schema and path. Each path can be connected by a remote user through a verb such as GET, POST, PUT, or DELETE. Combined with HTTP verbs and paths, the web application delivers web content.
In this recipe, we worked with GET requests (which are default ones) that are associated with different routes. In the Snap Framework, the route is a list of path/route names and the corresponding handlers. The route function takes this table and allows us to construct a site. The special function ifTop handles dealing with the root (/) path.
In the REST (https://en.wikipedia.org/wiki/Representational_state_transfer) philosophy, the path acts as a state, and hence, the path fragment can be a parameter uniquely determining a state. In the Snap Framework, a parameter is identified by the prefix ':', and the string that follows this name (till the path separator or '/' character) is the parameter name. The Snap Framework allows us to access this parameter using the getParam function.
We use the quickHttpServe function to run the built-in HTTP server. By default, it runs the HTTP server at port 8000.