Recently, I resumed work on my projects, one of which involved connecting native JavaScript with Node.js using HTTP calls and doing it live on an Ubuntu Nginx web-server.
When I first attempted this in May, I was completely lost. I couldn't figure out why it wasn't working and broke down and eventually gave up for a few weeks. I tried again with a fresher perspective, but still faced issues. After numerous attempts and 2 months, I'm proud to say that I've finally solved it.
The project was to create a dashboard page for my sister's domain, allowing her to redirect the domain to anything, whether it be her Medium page or a blog. The domain would stored in a JSON file and also allow for rewrite on the client side without her ever needing to ask me to do anything that would require server access.
To build this, I discovered I needed Node.js so as to write JSON files. I successfully managed to rewrite a file using Node.js, but struggled to connect Node.js and regular JavaScript. I got help and was advised to use HTTP calls. It worked perfectly on VSCode with localhost, but failed on my server with Nginx as the webserver and reverse proxy. I realized how frustrating the "it works on my machine" issue can be.
In a panic, I searched online and asked ChatGPT for help, but only ended up copying solutions blindly. I got frustrated and gave up. However, today, I finally made it work. I managed to make the JavaScript call the Node.js, which then rewrote the new redirect URL and made it live. I also implemented server-side password validation for security reasons and it works which boosted my confidence.
This time, I took it step by step. I first connected Nginx and Node.js, using modules and libraries like PM2. I set up a basic "hello world" response on Node.js and configured the server block with a proxy pass. It worked! Then, I added the rewrite code to the Node.js file and tested it. Finally, I connected it through JavaScript using a simple window.location command, and it worked perfectly.
Finally, I even did a little dance to celebrate. I'm thrilled with the outcome and proud of my persistence. This just made me realize to never back down and never give up.
I even plan to make a proper tutorial so if anyone ever needs to know how to do this (including me) it will help them. I am also going to make a youtube video just because i can.
Check this out on Medium.
Comments