13 Web programming

 

This chapter covers

  • The web and its relationship with functional programming
  • Web programming on .NET and F#
  • Server-side web development in F#
  • Client-side web development with Elmish
  • The SAFE Stack

This chapter will tie together even more of what we’ve seen so far, bringing both the functional paradigm and 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 briefly summarizes 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 many frameworks use OOP patterns as a way of modelling web applications such as Controllers. In such frameworks, you often create classes that have methods 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).

Figure 13.1 An HTTP call 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 HTTP Handlers

13.2.6 Reading inputs

13.2.7 Validation

13.3 Functional patterns in a web application