Creating a simple HTTP Server with F#

  • 1
  • April 20, 2011
Julian Kay

Julian Kay


    Following on from Doug’s post about Double Meta Refresh; we found it is quite handy to be able to visually test what the referrer is when visiting a link.

    Again, I call on my favourite language – F# – for doing quick scripts and utilities!

    At the top we have a function called listener which takes in a handler function clearly shown as the type HttpListenerRequest -> HttpListenerResponse -> Async<unit>

    If you’ve ever used HttpListener before, then you may recognise the code in the body of the listener function. It sets up then starts the HttpListener and creates an Async task for the HttpListener. Finally, we pass an anonymous function to listener which simply writes to the response output stream with the text from the output function just above it.

    The last few lines are required only if you compile the code. It simply ensures the console application does not exit immediately; if you’re pasting this example into F# Interactive, you can skip them.

    Read more posts about F#: