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
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...
using static SplashKitSDK . SplashKit;
WriteLine ( " About to start the server... " );
// Start a web server - defaults to listening to port 8080
WebServer server = StartWebServer ();
WriteLine ( " Waiting for a request - navigate to http://localhost:8080 " );
// Wait and get the first request that comes in
HttpRequest request = NextWebRequest (server);
// Send back the index.html file
SendHtmlFileResponse (request, " index.html " );
// For now, we are done, so let's shutdown
public static void Main ()
SplashKit . WriteLine ( " About to start the server... " );
// Start a web server - defaults to listening to port 8080
WebServer server = SplashKit . StartWebServer ();
SplashKit . WriteLine ( " Waiting for a request - navigate to http://localhost:8080 " );
// Wait and get the first request that comes in
HttpRequest request = SplashKit . NextWebRequest (server);
// Send back the index.html file
SplashKit . SendHtmlFileResponse (request, " index.html " );
// For now, we are done, so let's shutdown
SplashKit . StopWebServer (server);
write_line ( " About to start the server... " )
# Start a web server - defaults to listening to port 8080
server = start_web_server_with_default_port ()
write_line ( " Waiting for a request - navigate to http://localhost:8080 " )
# Wait and get the first request that comes in
request = next_web_request ( server )
# Send back the index.html file
send_html_file_response ( request , " index.html " )
# For now, we are done, so let's shutdown
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.
write_line ( " About to start the server... " );
web_server server = start_web_server ();
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 " );
//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... " );
using static SplashKitSDK . SplashKit;
WriteLine ( " About to start the server... " );
WebServer server = StartWebServer ();
WriteLine ( " Waiting for a request - navigate to http://localhost:8080 " );
// Get the next request that the server has
request = NextWebRequest (server);
WriteLine ( " I got a request for " + RequestURI (request));
if ( IsGetRequestFor (request, " /login " ) || IsGetRequestFor (request, " /login.html " ))
// Serve page for login path
SendResponse (request, " login page " );
// If no specified path is requested, serve index.html to the user
SendHtmlFileResponse (request, " index.html " );
WriteLine ( " About to stop the server... " );
public static void Main ()
SplashKit . WriteLine ( " About to start the server... " );
WebServer server = SplashKit . StartWebServer ();
SplashKit . WriteLine ( " Waiting for a request - navigate to http://localhost:8080 " );
// Get the next request that the server has
request = SplashKit . NextWebRequest (server);
SplashKit . WriteLine ( " I got a request for " + SplashKit . RequestURI (request));
if ( SplashKit . IsGetRequestFor (request, " /login " ) || SplashKit . IsGetRequestFor (request, " /login.html " ))
// Serve page for login path
SplashKit . SendResponse (request, " login page " );
// If no specified path is requested, serve index.html to the user
SplashKit . SendHtmlFileResponse (request, " index.html " );
SplashKit . WriteLine ( " About to stop the server... " );
SplashKit . StopWebServer (server);
write_line ( " About to start the server... " )
server = start_web_server_with_default_port ()
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 " )
# 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... " )
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:
gets the next request
processes the request
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.
write_line ( " About to start the server... " );
web_server server = start_web_server ();
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 " );
//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... " );
using static SplashKitSDK . SplashKit;
WriteLine ( " About to start the server... " );
WebServer server = StartWebServer ();
WriteLine ( " Waiting for a request - navigate to http://localhost:8080 " );
WriteLine ( " To end - navigate to http://localhost:8080/quit " );
// Get the next request that the server has
request = NextWebRequest (server);
while ( ! IsGetRequestFor (request, " /quit " ))
WriteLine ( " I got a request for " + RequestURI (request));
if ( IsGetRequestFor (request, " /login " ) || IsGetRequestFor (request, " /login.html " ))
// Serve page for login path, e.g.
// SendHtmlFileResponse(request, "login.html");
SendResponse (request, " login page " );
else if ( IsGetRequestFor (request, " /contact " ) || IsGetRequestFor (request, " /contact.html " ))
// Serve page for contact path, e.g.
// SendHtmlFileResponse(request, "contact.html");
SendResponse (request, " contact page " );
else if ( IsGetRequestFor (request, " /about " ) || IsGetRequestFor (request, " /about.html " ))
// Serve page for about path, e.g.
// SendHtmlFileResponse(request, "about.html");
SendResponse (request, " about page " );
// If no specified path is requested, serve index.html to the user
SendHtmlFileResponse (request, " index.html " );
WriteLine ( " Waiting for a request - navigate to http://localhost:8080 " );
WriteLine ( " To end - navigate to http://localhost:8080/quit " );
// Get the next request that the server has
request = NextWebRequest (server);
WriteLine ( " About to stop the server... " );
public static void Main ()
SplashKit . WriteLine ( " About to start the server... " );
WebServer server = SplashKit . StartWebServer ();
SplashKit . WriteLine ( " Waiting for a request - navigate to http://localhost:8080 " );
SplashKit . WriteLine ( " To end - navigate to http://localhost:8080/quit " );
// Get the next request that the server has
request = SplashKit . NextWebRequest (server);
while ( ! SplashKit . IsGetRequestFor (request, " /quit " ))
SplashKit . WriteLine ( " I got a request for " + SplashKit . RequestURI (request));
if ( SplashKit . IsGetRequestFor (request, " /login " ) || SplashKit . IsGetRequestFor (request, " /login.html " ))
// Serve page for login path, e.g.
// SendHtmlFileResponse(request, "login.html");
SplashKit . SendResponse (request, " login page " );
else if ( SplashKit . IsGetRequestFor (request, " /contact " ) || SplashKit . IsGetRequestFor (request, " /contact.html " ))
// Serve page for contact path, e.g.
// SendHtmlFileResponse(request, "contact.html");
SplashKit . SendResponse (request, " contact page " );
else if ( SplashKit . IsGetRequestFor (request, " /about " ) || SplashKit . IsGetRequestFor (request, " /about.html " ))
// Serve page for about path, e.g.
// SendHtmlFileResponse(request, "about.html");
SplashKit . SendResponse (request, " about page " );
// If no specified path is requested, serve index.html to the user
SplashKit . SendHtmlFileResponse (request, " index.html " );
SplashKit . WriteLine ( " Waiting for a request - navigate to http://localhost:8080 " );
SplashKit . WriteLine ( " To end - navigate to http://localhost:8080/quit " );
// Get the next request that the server has
request = SplashKit . NextWebRequest (server);
SplashKit . WriteLine ( " About to stop the server... " );
SplashKit . StopWebServer (server);
write_line ( " About to start the server... " )
server = start_web_server_with_default_port ()
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 not 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 " )
elif 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 " )
elif 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 " )
# 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... " )
The html for your web page would be:
< title > Basic HTML Page </ title >
< a href = " login " > Login Page </ a >
< a href = " contact " > Contact Page </ a >
< a href = " about " > About Page </ a >
Adding Links
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
< a href = " login " > Login Page </ a > < br />
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.