URLs and Paths
Learning objectives
- You know what paths are and can handle requests to paths.
The server that we previously studied is a HTTP server that responds to HTTP requests. Each request is made to a specific URL, which also has a path.
Requested URL
The URL that has been requested can be accessed through the property url
of the request
-object. Running the following application launches a server that responds to the user with the URL that the request is made to.
const handleRequest = (request) => {
return new Response(request.url);
};
Deno.serve(handleRequest);
We can test the printed URL either by using the browser or by using the curl
command. In the following example, we first make a request to the root path of the server, i.e. to http://localhost:8000
, after which make a request to the path /hello
on the server, i.e. to http://localhost:8000/hello
.
curl http://localhost:8000
http://localhost:8000/%
curl http://localhost:8000/hello
http://localhost:8000/hello%
In the above example where the curl
command is run within a terminal, the percentage-sign %
visible in the output depicts the lack of a line change at the end of the response. When trying out the request in a browser, no percentage sign is shown.
Requested paths
If we wish to use the path of the URL, e.g. /hello
from http://localhost:8000/hello
, we can use the URL-class to create an URL
object out of the url
variable representing the requested URL. The created object has properties through which we can access a variety of url parts, including the the path, which is stored in a property called pathname
.
The example below launches a server that responds with the requested path.
const handleRequest = (request) => {
const url = new URL(request.url);
return new Response(url.pathname);
};
Deno.serve(handleRequest);
curl http://localhost:8000
/%
curl http://localhost:8000/hello
/hello%
Path-specific responses
Normally, when we make requests to different paths of websites, we get different responses. Let's mimic this behavior through a simple example.
The following example creates a server that responds with the message world
to requests that are made to the path /hello
and with the message ingredient
to requests that are made to a path containing the string secret
. Any other requests receive a response with the message hello
.
const handleRequest = (request) => {
const url = new URL(request.url);
let message = "hello";
if (url.pathname === "/hello") {
message = "world";
} else if (url.pathname.includes("secret")) {
message = "ingredient";
}
return new Response(message);
};
Deno.serve(handleRequest);
We can, again, try out the application using the browser or the command line. In the following example, the application is tried out with a few curl
requests.
curl http://localhost:8000
hello%
curl http://localhost:8000/world
hello%
curl http://localhost:8000/helloes
hello%
curl http://localhost:8000/hello
world%
curl http://localhost:8000/secret
ingredient%
curl http://localhost:8000/secrets
ingredient%