13 Web Programming

 

This chapter covers

  • The Web and Functional Programming
  • Web Programming on .NET and F#
  • Server-side web development in F#
  • Client-side development with Elmish
  • The SAFE Stack

This chapter is one that’ll tie together even more of what we’ve seen so far, bringing both the functional paradigm as well as core abstractions such as Task-based asynchronous programming and Options to enable web programming that naturally fits with F#.

13.1 The Web and Functional Programming

This section gives a brief re-cap on HTTP and explains why (although this may surprise you) FP is a natural fit for web programming.

One of the fundamental underpinnings of the web today was the invention of the Hypertext Transfer Protocol (HTTP), which provided a standard messaging format for web traffic. These days we see lots of frameworks use OOP patterns as a way of modelling web applications such as Controllers. In such frameworks, you often create classes which have methods that are decorated in some way such that web requests are correctly “routed” to a method; you’ll often have to inherit from “base classes” which give extra functionality. All of this is especially unfortunate because it's just hiding what HTTP really is:

Figure 13.1 An HTTP closely matches a function call – a request payload followed by a response from the server.

13.1.1 Web Programming on .NET and F#

13.1.2 The HTTP Context

13.2 Server-side Web Development in F#

13.2.1 Creating our first ASP.NET Application

13.2.2 The HTTP Handler

13.2.3 Composing HTTP Handlers together

13.2.4 Composing multiple routes

13.2.5 Writing our own Handlers

13.2.6 Reading inputs

13.2.7 Validation

13.3 Functional patterns in a web application

13.4 Creating HTML views

13.4.1 Creating views through code

13.4.2 The ViewEngine DSL

13.4.3 Benefits of an F# DSL for HTML

13.5 Full Stack F# apps with SAFE Stack

13.5.1 Overview of SAFE Stack

13.5.2 Installing the SAFE Stack template

13.5.3 The MVU pattern

13.6 Summary