What is NodeJS?
-> Its basically javascript that runs on the machine.Originally, javascript was designed for running in web browser.(chrome, firefox, etc).
The makers of NodeJS made the runtime of Google chrome's V8 javascript engine to run on a machine.
So, now it can actually run in a terminal in any machine, just like php, ruby on rails, etc.
So, now we basically have full access to the computer and computer resources via the program.
NodeJS is primarily used for
- command-line utilities
- web servers
What does NodeJS do well?
- Lightweight Application all the way upto very large
There are really big companies using NodeJS, because of many reasons
- high concurrency
NodeJS scales-out very well to handle lots and lots of traffic.
- Asynchronous Actions/Requests/non-blocking
It is able to make multiple requests that are non-blocking in the same time(as javascript is made to handle asynchronous actions all the time).
-
running node in terminal is equivalent of running an interpreter, where we can run commands in real time.
console.log('hi');
alert();
doesn't work here as it used to do inside the browser as here there is no browser to alert to.
we can define functions, declare variables, do operations, just like in python interpreter.
> var a = 1;
> a + 2
3
> var b = function(){ console.log("hi");}
>b();
hi
Here in NodeJS, there is no "window" object as there used to be in javascript, because this runtime isn't attached to any browser window.
In javascript,
var a = 1;
meant window.a just got created.
In Node, we have
> global.a;
1
Also, there is no document object as well as navigator, which are usually found in every browser.
Instead we have a process object, which contains all the details of the processes that are running.
A process is simply a program running on a computer.
e.g. :- running google chrome creates a process, every tab has its own process.
process.exit();
exits the interpreter console and control returns to our bash terminal.