In this article, we will see how to construct a simple web application with the F# language and the Suave Web Server on Visual Studio Code.
Suave
Suave is a simple Web Server that can run web applications. It runs on Windows, OS X, and Linux and F# is the language used on it.
F# is a functional language created by Microsoft in 2002.
F# combined with Suave enables a set of combinators to manipulate the route flows and task composition. We can build a web application very easily and quickly with the F# syntax and Suave lightweight.

Install the tools
Let’s begin by installing all the tools to start our application. We will use Visual Studio Code, a free and open source editor where we can add extensions for building applications.
I love Visual Studio Code’s modularity because we can transform it into a very powerful work tool. You can download it here for free. The installation is very fast and simple. Once installed, open it.
We will now install F# tools in Visual Studio Code: on the left side, there is a vertical bar with five buttons.

Respectively
- A file and solution explorer
- A search section
- A source control (for tool like Git)
- A debugger
- And an extension manager
For installing F#, we will go to the Extension Manager. Just click on the last icon with your mouse.

Search for three extensions and click "Install" on them.
- Ionide F#: for the F# language support
- Ionide Paket: a packet managers like NuGet but for the F# project.
- Ionide FAKE: a tool for automate build like a Makefile but for F#.
After installations, restart Visual Studio Code. Now, we are ready to start.
Ensure to follow installation details so that F# can run properly on your computer.
Create a new F# suave project
Ionide will help us a lot by providing some start templates for create application. Like Visual Studio, you can generate a start project for a console app, library, or Suave.
In the first place, create a repository for you project on your computer. I will name it HelloSuave, for example. Then, open this repository with Visual Studio Code (menu File > Open repository).

Now, to create a project, we need to access the command pallet (Ctrl+Shift+P on Windows, ⌘+Shift+P on macOS, Ctrl+Shift+P on Linux) and type the following.

Select and press Enter on F#: New Project. Then, in the list of templates, select F# Suave project and press Enter again. When Visual Studio asks you to choose a directory, just leave the field empty and press Enter. Name the project as HelloSuave or whatever you prefer.
After a short time, all the files needed are generated.

All the project code is in HelloSuave (under –no-prompt) subdirectory. There are also two files which will interest us: paket.dependencies and build.fsx.
In the dependencies PAKET file, we get all packages needed to run a Suave application.

We actually have reference to FAKE, the automation build tool, F# Core, and Suave, all referenced in the NuGet source.
The build.fsx file act as a Makefile in F#, you can define some build action.

That will generate or install tools, or clean a project directory.
Now, let’s focus on Suave. We have actually one source file very simple for our web application - HelloSuave.fs with the following lines of code.
open Suave // always open suave
open Suave.Successful // for OK-result
open Suave.Web // for config
startWebServer defaultConfig (OK "Hello World!")
It simply starts the web server with default configuration (port 8083) and registers a WebPart OK “Hello World!”. A WebPart is basically a function that will be executed on the server. OK means the response status is 200 that means the request returns a good result.
Build and run the application
In Visual Studio Code, go to Menu Display > integrated Terminal. That will open a terminal window in your work directory.

On Windows, just type .\build.cmd and press Enter and on OS X and Linux, type ./build.sh. The build will take some time for downloading the packages and build the solution by following the steps in the build.fsx with FAKE tool.

If all is fine. We can now execute our server. The executable is located in –no-prompt\HelloSuave\bin\Debug\net461\HelloSuave.exe. Execute it via the terminal.

Now, our web application is running on the server at this address: 127.0.0.1:8080. Open a Web browser and enter the URL in the URL text field.

Perfect! It works fine.
Explain the code



Enkhdulguun DulguunPosted Aug 26, 2018, 12:00 PM
Hi i got this error after ./build.sh. /Users/enkhdulguun/.local/share/dotnetcore/sdk/2.0.2/Microsoft.Common.CurrentVersion.targets(1122,5): error MSB3644: The reference assemblies for framework ".NETFramework,Version=v4.6.1" were not found. To resolve this, install the SDK or Targeting Pack for this framework version or retarget your application to a version of the framework for which you have the SDK or Targeting Pack installed. Note that assemblies will be resolved from the Global Assembly Cache (GAC) and will be used in place of reference assemblies. Therefore your assembly may not be correctly targeted for the framework you intend. [/Users/enkhdulguun/Documents/HelloSuave/HelloSuave/HelloSuave.fsproj]
Mahesh ChandPosted Nov 23, 2017, 1:22 PM
Welcome to C# Corner, Arthur.