https://github.com/nsaunders/purescript-nodetrout
Build a Node HTTP server with Trout.
https://github.com/nsaunders/purescript-nodetrout
Last synced: about 1 year ago
JSON representation
Build a Node HTTP server with Trout.
- Host: GitHub
- URL: https://github.com/nsaunders/purescript-nodetrout
- Owner: nsaunders
- License: mit
- Created: 2019-09-20T15:22:42.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-11-04T18:19:36.000Z (over 5 years ago)
- Last Synced: 2025-02-28T23:17:06.152Z (over 1 year ago)
- Language: PureScript
- Homepage:
- Size: 111 KB
- Stars: 8
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# purescript-nodetrout [](https://travis-ci.org/nsaunders/purescript-nodetrout) [](https://pursuit.purescript.org/packages/purescript-nodetrout) [](https://github.com/purescript/package-sets)
## Build a Node HTTP server with Trout.

[Trout](https://github.com/purescript-hyper/purescript-trout) is a type-level routing DSL. Similar to Haskell's [Servant](https://github.com/haskell-servant/servant) library, Trout allows routes to be specified as a data type. Given this data type along with a [record](https://github.com/purescript/documentation/blob/master/language/Records.md) of corresponding handlers, this library can produce a [`node-http`](https://github.com/purescript-node/purescript-node-http) request handler of type `Request -> Response -> Effect Unit`, which [can then be used to create an HTTP server](https://pursuit.purescript.org/packages/purescript-node-http/5.0.2/docs/Node.HTTP#v:createServer).
### An API in 4 simple steps
1. Specify routes as a data type. Here, we create a `GET /admin` route that requires a Basic Authorization header and responds with a greeting:
```purescript
type Site = "admin" := "admin" :/ Header "Authorization" BasicAuth :> Resource (Get Greeting JSON)
```
2. Create a [`Proxy`](https://pursuit.purescript.org/packages/purescript-proxy/3.0.0/docs/Type.Proxy) value to capture the route specifications:
```purescript
site :: Proxy Site
site = Proxy
```
3. Define a handler for each route. Here, we greet the user by the username specified in the Basic Authorization header:
```purescript
resources :: forall m. Monad m => { admin :: BasicAuth -> { "GET" :: ExceptT HTTPError m Greeting } }
resources = { admin: \auth -> { "GET": pure $ Greeting $ "Hello, " <> (fst $ un BasicAuth auth) } }
```
4. Serve the API using `node-http`:
```purescript
main :: Effect Unit
main = do
server <- createServer $ serve' site resources (const $ pure unit)
listen server { hostname: "0.0.0.0", port: 3000, backlog: Nothing } $ log "Listening on port 3000..."
```
### Installation
[bower](https://github.com/bower/bower):
```
bower install --save purescript-nodetrout
```
[psc-package](https://github.com/purescript/psc-package):
```
psc-package install nodetrout
```
[spago](https://github.com/spacchetti/spago):
```
spago install nodetrout
```
### Examples
A number of usage examples are available [here](example).
To run the examples, clone the repository and run one of the following depending on your package manager and build tool, replacing `` with the name of one of the examples.
[bower](https://github.com/bower/bower) + [pulp](http://github.com/purescript-contrib/pulp):
```
bower install
pulp run -I example -m Example.
```
[spago](https://github.com/spacchetti/spago):
```
spago run -p example/.purs -m Example.
```