Introduction to Node.js
Let's build a simple introductory website that displays date and time
In this article, I will cover the basics of node.js and help you write your first code in Node.js. you may have heard about Node and are curious as to what it is and how powerful it can be, be it you are a newbie in programming or you are an experienced programmer and are looking for a change.
So, what is node.js?
Node.js is a server-side JavaScript runtime environment. Let’s break this down further to get a better grasp, understanding, and foundation of the basics. Server-side implies that the application runs on a computer’s operating system as opposed to a web browser or a phone. The latter in the definition (JavaScript runtime environment), implies that Node.js is an application that processes JavaScript commands and decides what commands should be executed and when and this is not all. The application then goes on to send these JavaScript commands to an embedded JavaScript engine i.e.
Chakra in Microsoft edge
Carakan used by the opera software before their move to the V8 engine
Spider Monkey used in Mozilla
JavaScript Core used in safari
V8 used in google chrome, node.js and deno.
Node.js is a C++ application that embeds the V8 js engine. Ryan dahl was the original developer of Node.js and the Deno JavaScript and TypeScript runtime but I will not focus on the history and biography of the software engineers that brought about this beautiful and powerful application in this article but rather a brief introduction.
Node.js presents itself as two applications:
A REPL (Read Evaluate Print Loop)
A script processor
The REPL
When you enter the node command in the terminal and you don’t precede it with a file name, you invoke the REPL. The REPL is a JavaScript interactive JS runtime. You can write any JavaScript here and it will be executed. Here, you can define a variable and even manipulate them easily. The REPL stages are simple:
R-read-reads the input from you the developer.
E-evaluate-executing your input systematically.
P-print-printing its response, or the return values of your functions.
L-loop-loops back to the top to scan for any changes in the code written.
The script processor
The script processor is simply invoked by the command node on the CLI. This will make sense when we start to write code.
i.e., node {document name}
this is to enable node to open the declared script and read its dependencies.
e.g., node app.js will open app.js script.
Installation and prerequisites
In order for us to get started, we will need to install the latest version of node.js from their website: https://nodejs.org/en/download/
Here, you will download the latest stable version of Node.js and install it. This is the simplest way of doing it.
Other ways of installing Node.js is by use of NVM (Node Version Manager) in your windows CLI and in my personal opinion, this is by far the best method because it lets you install several versions of Node.js and by use of simple commands such as nvm ls to list all the available and installed versions of node or even nvm install to install a certain node version. More on this later.
We will also need a code editor and a CLI to work with. I recommend using git bash but the VS code terminal also works perfectly.
After the installation of node, test that it has already been installed by typing node -v on your CLI to show you the installed version that you have.
Writing our first code
After the installation, create a new folder for your first project and name it ‘My first node project’. On the folder, open git bash and run the command npm install. This command is finding a JSON file named package.json in the root directory to install all dependencies defined in this file. On the same folder, create a file and save it as app.js. On it, write the following simple sample code:
var http = require ('http');
var dt = require('./server');
http.createServer(function(req, res){
res.writeHead(200,{'content-Type': 'text.html'});
res.write("The date and time are currently: " +dt.myDateTime());
console.log("Our app is running on port 3000");
res.end();
}).listen(3000);
Create a new file and save it as server.js and in it, copy the code below.
exports.myDateTime = function(){
return Date();
};
On Git bash (the same one you had already opened to check the node version), type node app and enter. Go to your favorite browser and go to the address http://localhost:3000/
Your current time and date will be displayed and anytime you reload the page, the time updates. Now, let’s inspect the code to see how it works.
In the first part of the code, we use an inbuilt module of Node.js: HTTP to create a global instance of the HTTP and to help manage connections, and Yes, that’s right, Node has inbuilt modules that help to simplify your work. some more examples of built-in modules include fs (file system module), crypto, path, URL, etc. We also exported the second script: server.js to be used in the first one: app.js and used the date and time function. This is a common practice in node and in most instances, you will see the declaration module.exports={script name} being used to transfer certain contents of file A to file B.
res.writeHead(200,{'content-Type': 'text.html'});
helps us to input the page content so that it can be displayed. Our hostname is localhost in this case and our port number is 3000 but this can change depending on the project you are working on and as you desire to edit it. You have now successfully created your first working project in Node.js.