Http Server in PureScript
Here’s the translation of the HTTP server example from Go to PureScript:
Our HTTP server example demonstrates how to create a basic web server using PureScript. We’ll use the purescript-express
library, which provides bindings to the Express.js web framework.
First, let’s set up our project and install the necessary dependencies:
Now, let’s create our server code in a file named src/Main.purs
:
In this PureScript version:
We define two handler functions:
hello
andheaders
. These correspond to the Go handlers but are implemented using PureScript’sApp
type from thepurescript-express
library.The
main
function sets up the Express application, registers the routes, and starts the server.We use
get
to associate paths with handlers, similar tohttp.HandleFunc
in Go.Instead of
http.ListenAndServe
, we uselistenHttp
to start the server.
To run the server:
You can then access the server using curl:
This PureScript implementation provides similar functionality to the Go example, showcasing how to create a basic HTTP server with multiple routes and custom handlers.