This must be something really stupid. I just started playing with scotty and cannot get the static content served correctly.
import Network.HTTP.Types
import Web.Scotty
import qualified Data.Text as T
import Data.Monoid (mconcat)
import Data.Aeson (object, (.=))
import Network.Wai.Middleware.Static
main = scotty 3000 $ do
middleware $ staticPolicy (noDots >-> addBase "static")
get "/" $ file "index.html"
Pretty simple. That's what you find in a couple of scotty tutorials. But it does not work for some reason. Accessing via '/' in the browser gives me the 'file not found' thing. If I type '/index.html' at the browser - it works. But it is wrong! I want it to be accessible via '/' but not '/index.html'. It disregards the root and picks up the html file directly. How can I serve index.html via '/' root? There is not much info around and a couple tutorials that I found point to the above example or similar which does not work as expected.
I tried setting the headers to 'text/html' and what not ... No luck.
If I type '/index.html' at the browser - it works. But it is wrong! I want it to be accessible via '/' but not '/index.html'.
But that's exactly what the staticPolicy
middleware is for! Whenever a request matches a policy (in this case files in ./static/), it will be filtered by staticPolicy
. If you don't want that behavior, remove the middleware.
Accessing via '/' in the browser gives me the 'file not found' thing.
Because the middlware action doesn't set the relative path for the following commands. You need to give the full path for file
:
main = scotty 3000 $ do
get "/" $ file "./static/index.html"