Dispatching requests
We have gone through the first two cases of Server.handler
. They are both simple and accept all requests. But in real web applications, requests and responses are much more complicated, and the web server should respond differently according to different requests. This section we will cover the remaining cases of Server.handler
and will show how to handle different types of requests.
The resource handler
A resource handler is often used to serve static resources such as images and sounds. The case {stringmap(resource) resources}
performs on a non-decoded URI (Uniform Resource Identifier), and returns a resource that the URI matches with the resource's stringmap. To make it clear, let's suppose there are three images in the directory res: opa1.png, opa2.png, and opa3.png
.
If we create the resources stringmap manually, it should look like this:
"res/opa1.png" -> a resource of opa1.png "res/opa2.png" -> a resource of opa2.png "res/opa3.png" -> a resource of opa3.png...