Skip to content

Routing With Servers

This guide is an intruduction to delivering different content based on route requested
Written by: Isaac Wallis and others
Last updated: October 2024


This article extends the skills gained from Getting Started With Servers specifically around serving different files for different paths

Routing

Routing is an important part of the web server’s job. This is how you get different pages depending on your requests for login, home, or about, pages on a site. Basically, a URL is structured [serverName]:[port]/[route], for example localhost:8080/login

Recap of servers

Start with this code, which we built in the introduction guide

#include "splashkit.h"
int main()
{
write_line("About to start the server...");
// Start a web server - defaults to listening to port 8080
web_server server = start_web_server();
write_line("Waiting for a request - navigate to http://localhost:8080");
// Wait and get the first request that comes in
http_request request = next_web_request(server);
// Send back the index.html file
send_html_file_response(request, "index.html");
// For now we are done... so lets shutdown...
stop_web_server(server);
return 0;
}

This has the following information we need:

  • server, the web server on port 8080 we will be working with
  • request, the HTTP request that we’re going to be sending responses to

Preparing Pages

You should have an index.html file in Resources/server in your project folder, which we served to users last time. Create 3 copies, and name them login.html, about.html, contact.html. Edit the content inside the <p> tags to reflect the new names. You don’t have to do anything too fancy, just add a message that identifies it as a different page. Your folder should now have the following files in it:

  • index.html
  • login.html
  • about.html
  • contact.html

Checking Requests

Checking to see if a request has been made for a certain path is really easy! SplashKit provides a Is GET Request For function that lets you pass in a path, and it lets you know if the request is for that path. Alternately, you can also use Request URI or Request URI Stubs if you want to dynamically respond to the details in the request. Is GET Request For returns a boolean, True or False, which we can use in an if statement to check each of the resources we want to serve.

The following code shows the use of an if statement to serve some login text or the index page, depending on what the browser requested.

#include "splashkit.h"
int main()
{
write_line("About to start the server...");
web_server server = start_web_server();
http_request request;
write_line("Waiting for a request - navigate to http://localhost:8080");
//Get the next request that the server has
request = next_web_request(server);
write_line("I got a request for " + request_uri(request));
if (is_get_request_for(request, "/login") or is_get_request_for(request, "/login.html"))
{
// Serve page for login path
send_response(request, "login page");
}
else
{
//If no specified path is requested, serve index.html to the user
send_html_file_response(request, "index.html");
}
write_line("About to stop the server...");
stop_web_server(server);
return 0;
}

Handling Multiple Requests

You’ll usually want your server to handle lots of requests, not just one then quit. The easiest way to do this is using a while loop that:

  1. gets the next request
  2. processes the request
  3. sends an appropriate response

You can then add a special route that will stop the server.

The following code illustrates the use of the concepts covered so far. You can now make multiple requests in your browser, and you can navidate to http://localhost:8080/quit to get the web server to stop.

#include "splashkit.h"
int main()
{
write_line("About to start the server...");
web_server server = start_web_server();
http_request request;
write_line("Waiting for a request - navigate to http://localhost:8080");
write_line("To end - navigate to http://localhost:8080/quit");
//Get the next request that the server has
request = next_web_request(server);
while ( ! is_get_request_for(request, "/quit") )
{
write_line("I got a request for " + request_uri(request));
if (is_get_request_for(request, "/login") or is_get_request_for(request, "/login.html"))
{
// Serve page for login path, e.g.
// send_html_file_response(request, "login.html");
send_response(request, "login page");
}
else if (is_get_request_for(request, "/contact") or is_get_request_for(request, "/contact.html"))
{
// Serve page for contact path, e.g.
// send_html_file_response(request, "contact.html");
send_response(request, "contact page");
}
else if (is_get_request_for(request, "/about") or is_get_request_for(request, "/about.html"))
{
// Server page for about path, e.g.
// send_html_file_response(request, "about.html");
send_response(request, "about page");
}
else
{
//If no specified path is requested, serve index.html to the user
send_html_file_response(request, "index.html");
}
write_line("Waiting for a request - navigate to http://localhost:8080");
write_line("To end - navigate to http://localhost:8080/quit");
//Get the next request that the server has
request = next_web_request(server);
}
write_line("About to stop the server...");
stop_web_server(server);
return 0;
}

The html for your web page would be:

<html>
<head>
<title>Basic HTML Page</title>
</head>
<body>
<p>
<a href="login">Login Page</a>
</p>
<p>
<a href="contact">Contact Page</a>
</p>
<p>
<a href="about">About Page</a>
</p>
</body>
</html>

You can add links to your index.html to link to the other pages we have created. The general syntax for links is

<a href="pathToLinkTo">Text to be clicked</a>

For example, a link to the login page would look like

<p>
<a href="login">Login Page</a> <br />
</p>

Add links to contact and about to index.html in this same manner, then try playing around with localhost:8080 in your browser of choice, checking out all the different pages.

What next?

With these concepts you should have enough to create a simple web server. You could make this more dynamic by reading details from the path in the URI, and then storing details within the program or in an associated database. The main thing to remember is that when you build a web server you need to wait for an incoming request, do some processing, and return a response.