DEV Community

Cover image for Writing API in Nim
Ethosa
Ethosa

Posted on • Updated on

Writing API in Nim

Why Nim 🤔

Nim is powerful programming language that compiles into C, Cpp, ObjC and JS.

You can install it here

HappyX ✨

HappyX is an asynchronous web framework, written in Nim.
Source code
We'll work with it.

nimble install happyx
Enter fullscreen mode Exit fullscreen mode

Getting Started 👔

You should create project. Choose project name and mark project type as SSG.

hpx create
Enter fullscreen mode Exit fullscreen mode

You should move into project folder after this.

cd PROJECT_NAME/src
Enter fullscreen mode Exit fullscreen mode

Development 🛠

Let's launch project.

nim c -r main
Enter fullscreen mode Exit fullscreen mode

Also you can use flags -d:httpx and -d:micro to use alternative HTTP servers.

Let's change main.nim. For it you should open src/ folder and open main.nim file. You will see 👀

import happyx

serve("127.0.0.1", 5000):
  get "/":
    "Hello, world!"
Enter fullscreen mode Exit fullscreen mode

Rewrite this 👨‍🔬

import happyx

# Go to http://127.0.0.1:5000/ 👨‍🔬
serve("127.0.0.1", 5000):
  # Assignment counter
  var counter = 0

  # You can open / in your browser and see result 🍍
  get "/":
    inc counter
    # Return string
    "counter = {counter}"

  # You can open /upd or /upd100 in your browser and see result 🍍
  get "/upd{i?:int}":
    counter += i
    "counter = {counter}"
Enter fullscreen mode Exit fullscreen mode

And recompile 🛠

That's all! Good Luck! 🙂

Also you can read about SPA in Nim

Top comments (0)