Newer
Older
labs / tiddlers / content / labs / lab07 / $__Labs_07_The Static Asset Module.md

We need to tell Jooby which static assets it is allowed to serve. We will create another module for doing this.

Create a new class named StaticAssetModule in the web package (in the <> source folder). The completed module looks like:

public class StaticAssetModule extends Jooby {

    public StaticAssetModule() {

        // handle favicons (silent 404)
        get("/favicon.ico", Route.FAVICON);

        // serve anything that matches a file in the static folder
        assets("/*", Paths.get("static"));
    }
}

The get is there to allow the server to handle requests for favicons (the little icons that sometimes appear in the browser tabs). Without this we would see a lot of 404 errors in our server output console when the browser requests a favicon from our server. This route will return a 404 error to the browser to tell it that we don't have a favicon, but will not produce a 404 error in the log output.

The assets line will tell Jooby that it can serve any file that exists in the static folder. This includes files in sub-folders inside static.

Add the following to the constructor of the Server class to register the module:

mount(new StaticAssetModule());