Search code examples
perlmojolicious

Unable to load images with Mojolicious web server


It would seem that I'm not able to display images with Mojolicious in the web browser. My source code of my Perl script looks like this:

#!/usr/bin/perl

use Mojolicious::Lite;

get '/' => sub {
  my $c = shift;
  $c->render(text => "Hello World! This is an image: <img src=\"logo.png\"> ");
};

app->start;

When I load: http://127.0.0.1:3000/ the 'Hello World' text is displayed but the logo.png is not displayed - logo.png appears as a broken image as the web browser reports it is missing. The logo.png file is inside the same directory I'm running the above script from.

What must I do to get images and other external static files like javascript files, css, etc. to load inside the web page.


Solution

  • By default, Mojolicious serves static from the public subdirectory of your application. Try putting the image file in there. You can change the static file directory using $app->static.