Skip to main content

NodeJs 101 : A Quick Look

Unless you have been living in a cave in the past several years, you must have heard something about "NodeJS" or just "Node" by now.

So what is it and what's all the noise is about?

In short, it is JavaScript running outside the web browser. Wait,.... what? ....... JavaScript has always been running in a web browser, hasn't it?

Well, not anymore since 2009. Now it runs outside the web browser, but where? .... On the Google's V8 engine. (Sometimes it is called the Chrome V8 engine). V8 engine can convert your JavaScript code to machine code and run it out side the browser.

Now don't ask "So What? .....". Because this means, if you are developing on a web application, now you can add stunning behaviors in the client side, as well as complete all the server side scripting with JavaScript.

This is the reason for the aforementioned noise, and the reason for JavaScript to become one of the most highest demanding skill in the past several years.

To get started with NodeJs, you need to install it first. Go to nodejs.org, and download the installer.

Once the installation is complete, start the command prompt.

Now lets go through some basic Node commands.
01. "node -v"
This command will give you the current version of Node installed. If this command works, that simply means NodeJs has successfully installed on your system.

02. "node"
This command will start a new node process, and keep waiting for JavaScript. For example, now you can type some valid JavaScript like the following and get it complied.
Just so if you failed to notice, important thing here is that we are running JavaScript, NOT in a browser. This new node process is actually running on the V8 engine.

We can quit the process  by pressing "Ctrl + C" twice, or with the command "process.exit(0)"
03. ''node <filename>.js"

3.01. Create a folder called "NodeTest" somewhere in your hard drive.
3.02. Open any code editor which you like. (I am using "Atom")
3.03. I am explaining with Atom editor here, but this can be any editor. Click on "Open Project"and select the "NodeTest" folder in the file browser.
3.04. Right click on the "NodeTest" folder in the "Projects" panel and choose "New File"
3.05 Name the new file as "test.js" and type the following code in it. You are very much welcome to substitute your name instead of mine.

console.log('Hello World');
var myName = "Indunil Ranawake" ;
console.log(`My name is ${myName}`); 


3.06. Now, brows into this "NodeTest" folder in the command prompt (or Terminal in Lynux)
3.07. type the command "node test.js" ("test.js" is the name of the file you created) and hit return key. This should execute the code and following result should be displayed in the terminal.

This means the following.
01. Node is successfully running in your machine.
02. Now you are capable of creating independent JavaScript files and run them outside a browser, much the same way you run a Java or C++ code.

The possibilities are endless !

We will explore as much as we can, in the coming posts.

Happy Learning :D
Indunil Ranawake 




Comments

Popular posts from this blog

Javascript 105 : Callbacks

Javascript is asynchronous. That means you cant guarantee that the code you write will always execute sequentially. Take the following code for example, Output When looking at the output, it is understandable that the statements has been executing sequentially.  But if I do a modification to the code like the following, the output will be different.  Output:  Don't worry about the line number 6 yet. the amendment there will just delay the the execution of the statement "console.log('statement 01')" by 100 milliseconds. Now the "statement 01" prints out at the end of the line. The change of the output can be explained as follows. the JS interpreter sees the line 6 (which is the first line of the script), and realize that it has to wait for a 100 milliseconds before executing the statement "console.log('statement 01')", but sees that other four lines has no such delay and can be executed right away. So it goes a...

Javascript 102 : Understanding 'this' keyword

The keyword "this" is frequently misunderstood when coding with Javascript. It is because its behavior depends on the execution context. Now what is "execution context"? It is mainly of three types. Global Execution Cotnext (GEC), Functional Execution Cotnext (FEC) and Eval. In GEC, it is the global environment which Javascript  (JS) is running. If you are running the script in a browser and you use the "this" keyword, it will refer to the "Window" object. But if you are using NodeJS and running the script outside a browser the "this" keyword would refer to the "Process". In FEC the "this" keyword would refer to the execution context created by the code inside the function. EVAL is hen you use the "this" keyword inside an "eval" function. If this does not make sense, don't worry because we are going to understand with an example which is always the best way. Make the following HT...