Home »
Perl
How to serve a static file in Perl Dancer?
In this tutorial, we will learn how we can serve static files like html in a route using Perl dancer web framework?
Submitted by Godwill Tetah, on December 11, 2020
I will show you how this can be accomplished on Perl dancer. I will create a route where our server will serve a web page on that route when requested.
Let's first get started with our html file. We will set up something simply.
Index.html
<!DOCTYPE html>
<html lang="en">
<head>
<style>
body {
background-color: yellow;
}
p {
color: blueviolet;
}
</style>
<title>HOME</title>
</head>
<body>
<center>
<p>I love dancing with perl dancer</p>
</center>
</body>
</html>
Now let's set up a route where the html file above will be served when a get request is made on that route. Before that, let me explain what happens; Perl dancer looks up the static file in a public folder. But we will set our own location to a folder called static. Create a new folder called static.
You will move the html file to the folder called static. We will then create a route know as index where our html file will be server. This is done using send_file in our perl file.
index.pl
use Dancer;
set public => path( dirname(__FILE__), '/static' )
; #setting public folder to static folder
get '/' => sub {
send_file '/index.html';
};
dance;
Run and see output!
Output: