DEV Community

Cover image for Hugo - sending hugs you really feel - The backend
Nagy Szabolcs
Nagy Szabolcs

Posted on

Hugo - sending hugs you really feel - The backend

The API 🤖

For the backend, I decided to go with a Node and Express combination.
My idea is to have 3 endpoints

Endpoint Behavior
GET api/:id responds with the given hug's data
POST api/?pattern={}&name="" adds a new hug to the database
POST api/:id increments the number of plays in the database

and to make calls from the HTML page to these.

Coding the backend was pretty straightforward because I worked with this kind of environment before.
The one issue I ran into multiple times tho was to do with CORS when connecting to the api from localhost. This time the api was on the DigitalOcean servers so I expected to encounter some issues.
I fixed these by adding this regex rule in the DigitalOcean panel .* which basically enables every source.

The database 🛢

I initially wanted to use a NoSQL database, but since I was running out of time quickly, I decided to go with PostgreSQL because that was the only option on the app platform.

I worked with SQL databases before, but one new thing I learned about was the JSONB Datatype. Turns out, you can conveniently store JSON data in traditional SQL databases. 😅
At first, I actually tried using JSON instead, but since I saved my patterns in a JSON array it couldn't be done.

Final thoughts 💭

Overall I managed to finish everything in a day or so!

Oh and, I haven't told you about the JavaScript behind the websites. Honestly they are pretty boring, just getting states from buttons and API calls, nothing fancy.

Anyways take care, and have a hug! 👻
hug gif

Top comments (0)