All Quick Start examples will use esy and dune for dependency management and build. These are not requirements of naboris.

Basic Server

An http server with minimal configuration.

# Project Structure

For this example we will assume the project directory structure looks something like this:

  root-project-directory/
  ├── lib/
  │   ├── dune
  │   └── App.re  # or App.ml for ocaml
  ├── dune-project
  └── package.json

# Esy package.json

If you are unfamiliar with esy it is a dependency management tool for OCaml projects. It uses a workflow similar to npm in the Node.js world.

Start with a very simple package.json file:

1{
2  "name": "App",
3  "version": "0.0.1",
4  "description": "Basic naboris example server",
5  "esy": {
6    "build": [
7      "dune build -p #{self.name}"
8    ],
9    "buildInSource": "_build"
10  },
11  "scripts": {
12    "install": "esy install",
13    "build": "esy b dune build @install",
14    "clean": "esy b dune clean"
15  },
16  "dependencies": {
17    "@opam/dune": "*",
18    "@opam/lwt": ">=5.1.1",
19    "@opam/naboris": "*"
20  },
21  "devDependencies": {
22    "esy": "^0.5.6",
23    "ocaml": "~4.7",
24    "reason-cli": ">=3.3.3"
25  },
26  "peerDependencies": {
27    "ocaml": ">=4.7.0"
28  }
29}

# Dune Files

If you are unfamiliar with dune it is a very commonly used build tool for native OCaml and ReasonML projects.

Our project need two dune files one at the project root called dune-project:

1(lang dune 1.6)
2(name app)

And one in the lib folder with our other source files called dune:

1(executable
2	(name app)
3	(libraries lwt naboris)
4)

# App Code

1/* lib/App.re */
2let startServer = () => {
3  let port = 9000;
4  let serverConfig = Naboris.ServerConfig.create()
5    |> Naboris.ServerConfig.setRequestHandler((route, req, res) =>
6        switch (Naboris.Route.meth(route), Naboris.Route.path(route)) {
7          | (GET, [""]) =>
8            Naboris.Res.status(200, res)
9              |> Naboris.Res.text(req, "Hello world!");
10          | _ =>
11            Naboris.Res.status(404, res)
12              |> Naboris.Res.text(req, "Not Found.");
13        });
14
15  Naboris.listenAndWaitForever(port, serverConfig);
16}
17
18Lwt_main.run(startServer());
1(* lib/App.ml *)
2let start_server ()=
3  let port = 9000 in
4  let server_config = Naboris.ServerConfig.create()
5    |> Naboris.ServerConfig.setRequestHandler(fun route req res ->
6      match (Route.meth route, Route.path route) with
7        | (GET, [""]) ->
8          Naboris.Res.status 200 res
9            |> Naboris.Res.text req "Hello world!"
10        | _ ->
11          Naboris.Res.status 404 res
12            |> Naboris.Res.text req "Not Found.") in
13
14  Naboris.listenAndWaitForever port server_config
15
16let _ = Lwt_main.run(main ())

# Build and Run

1$ esy install # install all required dependencies
2$ esy b dune build lib/App.exe # build the app
3$ esy b dune exec lib/App.exe # run the app
Support us on GitHub

Star, follow, fork

Star Fork

Found a typo? a bug? or something that just doesn't make any sense? Help improve these docs by opening a github issue.

naboris source code is licensed MIT.
It can be used, copied, and modified free of charge. However, the software is provided "as is" without any warranties. Click the link above for more information.