If you are in to web development or have been browsing the job boards for software engineering positions lately you’ve most likely come across mention of the term ‘Node’ or ‘NodeJS’. This blog post will seek to cover the history of this technology, a bit on how it works, and what it is used for today.
What Is NodeJS?
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
or by using a the following wget command
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
After this is complete you can then use nvm to install NodeJS simply by entering the following into your terminal:
nvm install node
After this is complete you should check your version of NodeJS in a new terminal by entering
Now that we know the general history of NodeJS and how to install it using nvm, lets look at some examples of how it is used.
NodeJS As Local Build Tools
In order to exit this environment simply press CTRL+C twice. It is important to note that each session you enter this environment is completely new and all previously defined variables or functions will be lost. You can also use NodeJS to interact with your file system. Lets do so by opening a text editor (I use VS code) and doing a little exercise where we use NodeJS to create a text file. First navigate to a directory you’d like to work in and create a file called app.js. Within it we will use two utilities provided by NodeJS to generate this text file. First we will create a variable set to require and pass it an argument of ‘fs’ which is a module in NodeJS that allows it to interact with our file system. We will then use this to call writeFileSync and pass it strings representing the file we’d like to creates name, and the content of the file.
If you save this and open a terminal within the same directory you can use node to execute this file by entering node followed by the name of the file (extensions optional)
this will create hello.txt within the same directory. If you open this new file you should see the text Hello Node.js World! Go ahead and try it out!
NodeJS For Server Side Code
The more popular usage for NodeJS is to create and manage a web server. One of the advantages NodeJS offers in this area is that it processes everything in an asynchronous non-blocking fashion. It does this with a single thread that continuously checks the call stack to see which functions need to be run and which have finished and need to be returned. This process is known as the ‘event loop’ good resource on the NodeJS event loop can be found within this article here. This allows many processes to be firing at the same time and while handling all of this logic at once is a tad heavier on hardware, it allows better scalability and a quick response.
Now that we have a general idea of what’s going on in the background lets set up a single page web server with NodeJS. Navigate to a folder you would like to work in and lets again create a file called app.js to open up within your text editor. Within app.js we will once again set a variable (this time called http) to the module returned by using require. This time however we will pass require the string of ‘http’ to signify what we will be working with the http module in NodeJS. We will then set another variable called server and set it to our http variable calling the createServer method provided by the http module in NodeJS. We will pass this createServer method an anonymous function which handles what we do with our request and response. Finally we will use this server variable to call the listen method on the port we would like to host our server on. The whole file should look like the following:
If you again navigate to the folder where app.js is and open a terminal you can run this file just as before by entering:
This will begin our server. If you then opened up your browser and navigated to localhost:3000 you will be greeted by a header reading “Hello NodeJS World!” with the title of “My NodeJS Page”. If you were to then navigate back to the terminal you used to run your server you will see a log containing a whole lot of information pertaining to your request. You can exit this process by pressing CTRL+C on your keyboard. Congratulations! If you followed along you just hosted your first NodeJS server!
Of course NodeJS is not only limited to the simple task of serving a static web page like what we just did. It can also validate input, connect to a database, return json, and much more. These are topics I will most likely cover in future blog posts, but in the mean time feel free to utilize the documentation to review this functionality for yourself. I hope this has provided a basic understanding of what NodeJS is and how it is used. Thanks for reading and happy coding!