Skip to content

Serving static content

While Fiction is mainly focused on BashX, it has support for many other content types and routes

You can serve files content using fiction.serveFile function:

Terminal window
fiction.serveFile "<file>" "<routeAlias?>" "<content type?>"

This function creates a route which points to function that returns file’s contents with it’s type

You can serve static HTML the same way by using text/html as content type

Terminal window
fiction.serveFile "index.html" "/" "text/html"
fiction.server "0.0.0.0:8080"

This code would show index.html as HTML (text/html) on root route (/)

Fiction supports directory file listing using fiction.serveDir

Let’s say we have directory test:

  • Directorytest/
    • test.txt
    • test.png
    • index.sh
Terminal window
fiction.serveDir "./test" "/files" false true

This would create following routes:

  • /files: file tree of ./test
  • /files/test.txt: contents of test.txt
  • /files/test.png: image
  • /files/index.sh: contents of index.sh