Background
Web services or microservices are used to refer to parts of a website that are run by scripting tools or languages like node.js. The proxy_pass directive in Nginx lets you configure Nginx to expose your web application to the world while keeping privacy control. If a node script is listening on port 8080, for example, Nginx handles the HTTPS requests and passes them by proxy to your script on 8080. The server runs a Node.js application managed by PM2 and provide users with secure access to the application through an Nginx reverse proxy.
Build a “hello world” server
Start by downloading node.js and the npm package manager and install the build-essential package. This screen shot shows two terminals running at the same time. The first is running the “hello.js” node script. The second terminal window is connecting to the localhost at port 3000 using the “curl” function. It got a “Hello World!” statement in response from the node script.
Next, I installed PM2 which is a process manager for node.js applications. This manager makes it possible to daemonize applications so that they will run in the background as a service. Applications that are running under PM2 will be restarted automatically if the application crashes or is killed. Below is the output from PM2 which shows that my “hello.js” script is currently running.
I also created a systemd unit that runs pm2 for your user on boot which will run my “hello.js” script whenever my host is running.
Now that my application is running on localhost, I setup reverse proxy server so that it can be accessed from my site. Accessing my url via a web browser will send the request to my hello.js file listening on port 3000 at localhost.
Setup location blocks
I can add additional location blocks to the same server block to provide access to other applications on the same server! By modifying the “sites-available” file for priyankais.online like this makes it so that I can access the hello.js script from priyankais.online/hello.
Getting Tom’s NodeExamples running
Start by “cd”-ing into the NodeExamples directory on the host and run a “sudo git pull” to update the local files. I’m going to be working with the getPost.js example in the “ExpressIntro” folder. Repeat the same steps from the section above:
I found this helpful site that describes URL search parameters and so I figured out how to use the getPost script. In the search bar you can type something like this: “https://priyankais.online/data?name=prinki&age=69” and the html will update according to our script.
Create a custom microservice for my site
Now that I know how the getPost.js script works, I’m going to edit it to do something … interesting?! To me? Using Tuan’s code as a reference, I updated the script to take parameters from the URL and create a response.
Below is my updated getPost.js script. I’ve also uploaded it github. You can reach it at priyankais.online/initiation.
Questions / Definitions
Sometimes I need to restart my application using pm2, why is that? I thought we configured it to always be running…
Express.js = a library that simplifies the making of RESTful interfaces
Curl = a command line tool that developers use to transfer data to and from a server. At the most fundamental, cURL lets you talk to a server by specifying the location (in the form of a URL) and the data you want to send.
Resources
https://itp.nyu.edu/networks/setting-up-restful-web-services-with-nginx/
https://adamtheautomator.com/nginx-proxypass/
https://developer.ibm.com/articles/what-is-curl-command/
https://flaviocopes.com/urlsearchparams/
https://github.com/tuantinghuang/und-net-F22/blob/main/simple-node/server.js