JAVASCRIPT JOURNEY - DAY 16
CHAPTER 16 : NODEJS -> FINALE
Node.js is nothing but JavaScript running on the server and it’s super-power!
We know that JavaScript is essential in/for a browser. But if you think about JavaScript engine alone, it can exist anywhere.
You can take the V8 JavaScript engine and install it on your computer.
SERVER-SIDE JAVASCRIPT? What? Is It possible.
It will possible look/visualized like taking any JavaScript engine, wrapping it inside an application that gives a clean interface to take the user’s JavaScript code and execute it in the JavaScript engine.
What? Gibberish It Sounds?
Only One Answer: NODEJS
WHAT THE HECK IS NODEJS
Node.js is also called simply Node or node.
Node.js is a framework that contains the V8 JavaScript engine, the standard library of packages, and some binaries.
When you install Node.js, you get node and npm binaries added to your path. That means, now you can use node and npm command.
Remember:
It is an actual myth that JS runs in browser , but there exists a compiler as well that does a lot of intermediate work before placing the code in an executable format in browser just like any other programming language.
V8 and Spider Monkey are one of the JS Engines. NodeJS is one such implementation of JS Engines that converts Standalone JS code.
There is no need to attach JS into HTML and hit reload each time..
WHAT IS MODULE EXPORTS THEN?
Normally our application is broken down to different parts.
When you import a file inside another file, that file is called a module. Node.js uses the Common JS module system syntax to import modules and packages.
Example:
There is a math.js file. This file contains some common math functions like add and multiply.
We will import this file inside calculate.js. Hence math.js is a module, since we are importing it and not executing it directly with Node.
To import a module, we need to use require function provided by the Node.js and available globally everywhere in the program.
This function takes a relative or an absolute path of the module file and returns what module is exporting.
// calculate.js
var math = require( './lib/math.js' ); // `.js` is optional
console.log(math); // {}
Add:
// add `add` function to `exports`
exports.add = function( num1, num2 ) {
return num1 + num2
};
From math module, we are exporting add function which returns the sum of the two numbers (arguments).
// calculate.js
var math = require( './lib/math' );
console.log(math);
{ add: [Function] }
FINAL:
// calculate.js
var math = require( './lib/math' );
// add 1 + 2
var result = math.add(1, 2);
console.log( result ); // 3
NPM : THE ULTIMATE WINNING HERO
When we installed Node.js, we also got npm command. NPM or Node Package Manager is the default package manager for Node.js.
The letters npm stand for “node package manager”.
Some examples of npm packages are: Angular React jQuery Express Socket.io
To use npm packages in a project, your project must contain a file called package.json.
This file keeps a list of all the packages you are using, and which version of each one you have chosen to use.
Thankyou! I hope now ejs6 is on our mind:-) Happy Learning!