Writing Simple RestAPI in Nim With HappyX #1

ethosa

Ethosa

Posted on May 22, 2023

Writing Simple RestAPI in Nim With HappyX #1

In this tutorial series we create RestAPI in Nim using HappyX web framework ✌

Getting Started 🔨

At first we need to install Nim lang

After this via nimble we install HappyX 🔌

nimble install happyx@#head
Enter fullscreen mode Exit fullscreen mode

I will edit my code with VS Code ✌

Project Creation 🛠

Just run this command

hpx create --name:tutorial --kind:SSG
cd tutorial/src
Enter fullscreen mode Exit fullscreen mode

Hello, world! ✌

Let's open tutorial/src/main.nim
You'll see this (without comments)

import
  # This will import HappyX framework
  happyx


# Setting up our server at `127.0.0.1:5000`
serve("127.0.0.1", 5000):

  # on GET HTTP method at 127.0.0.1:5000/
  get "/":
    # We'll see "Hello, world!"
    "Hello, world!"
Enter fullscreen mode Exit fullscreen mode

Let's rename route

  # on GET HTTP method at 127.0.0.1:5000/hello-world
  get "/hello-world":
Enter fullscreen mode Exit fullscreen mode

Now let's try it 🎈

nim c -r -d:debug main
Enter fullscreen mode Exit fullscreen mode

Open browser and go to http://localhost:5000/hello-world 🥳

Source code

💖 💪 🙅 🚩
ethosa
Ethosa

Posted on May 22, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related