Question

I want the user to be able to visit myserver.com and come to the index route.

And I want to show the list page when they visit myserver.com/pages.

However if the user visits an invalid url such as myserver.com/invalidpath/blabla it falls through to the indexPage route.

I want to respond with a 404 page when a invalid url is visited.

Here is an example of my routes:

  1 routes :: ServerPart Response                                                                                                                                                                                   
  2 routes = msum                                                                                                                                                                                                   
  3     [ dirs "pages/delete" $ delete                                                                                                                                                                              
  4     , dirs "pages/edit"   $ edit                                                                                                                                                                                
  5     , dirs "pages/save"   $ save                                                                                                                                                                                
  6     , dirs "pages"        $ listPages                                                                                                                                                                           
  7     , indexPage]  

What I thought about for now is that in my indexPage function I can check the path if it contains anything or if it is empty. This way I can determine if the fall through is from an invalid url or a index page visit.

Is there a good way to do this?

Was it helpful?

Solution

While writing this answer I found the solution in the documents on hackage.

There is a function called nullDir which you use for when the path is empty.

Using this function my example routes would look like this:

  1 routes :: ServerPart Response                                                                                                                                                                                   
  2 routes = msum                                                                                                                                                                                                   
  3     [ dirs "pages/delete" $ delete                                                                                                                                                                              
  4     , dirs "pages/edit"   $ edit                                                                                                                                                                                
  5     , dirs "pages/save"   $ save                                                                                                                                                                                
  6     , dirs "pages"        $ listPages                                                                                                                                                                           
  7     , nullDir >> indexPage
  8     , 404Page]  

Now the index page is only shown if the path is empty, and the 404 page is shown for all other fall throughs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top