What is Node.js and Why Node.js
- Node.js is an open-source server environment
- Node.js works on a variety of platforms (Windows, Linux, Unix, Mac OS X, etc.)
- Node.js uses JavaScript on the server. (Node.js is a JavaScript working time environment.)
A common web server function would be to open a file on the server and restore the content to the client:
Here's how PHP or ASP handles file requests:
- Sending activity to the computer file system.
- Waiting while file system opens and reads the file.
- Returns content to the client.
- We are ready to handle the next request.
Here's how Node.js handles file request:
- Sending activity to computer file system.
- We are ready to handle the next request.
- Once the file system has opened and read the file, the server returns the content to the client.
Node.js removes the wait, and simply moves on to the next request.
Node.js uses a single, non-blocking, asynchronously, system that works well in memory.
Node operating environment includes everything you need to create a JavaScript-enabled program.
Node.js came about when the original JavaScript developers stretched it from something you could only use in a browser to something you could use on your machine as a standalone program.
Now you can do more with JavaScript than just sharing websites.
JavaScript now has the ability to do what other scripting languages like Python can do.
Both your JavaScript and Node.js browser run on the V8 JavaScript search engine. This engine takes your JavaScript code and converts it into a fast machine code. Machine code is a standard code that a computer can use without having to translate it first.
Here is the official description as given on the official website of Node.js "Node.js"
Node.js uses an event-driven, non-blocking I / O model that makes it easy and efficient.
The Node.js' ecosystem package, npm, is the world's largest open source library system
Imagine a situation where we ask for a backend database of user1 and user2 data and print it on the screen / console. The response to this request takes time, but both user data requests can be performed independently at the same time.
Blocking I / O
In block mode, user2 data request is not initiated until user1 data is printed on the screen.
If this were a web server, we would have to start a new thread for all new users. But JavaScript has one thread (not really, but it has a single event loop, which we'll talk about later). This can therefore make JavaScript unsuitable for multi-thread operations.
That’s where the blocking part comes in.
Non-blocking I / O
Alternatively, using a non-blocking application, you can start a user2 data request without waiting for a response to user1 request. You can start both applications alike.
This non-blocking I / O eliminates the need for multiple threads because the server can handle multiple applications at once.
JavaScript event loop: -
Here's a quick step-by-step guide on how JavaScript Event Loop works:
- Push the main () into the call stack.
- Push console.log () in the call stack. This then runs instantly and comes out.
- Push setTimeout (2000) stack. setTimeout (2000) is a Node API. When we call it, we register the event return pairing. The event will wait for 2000 milliseconds, then reverting is a task.
- After subscribing to APIs, setTimeout (2000) appears in the call stack.
- Now the second setTimeout (0) is registered in the same way. We now have two Node APIs waiting to be released.
- After waiting 0 seconds, setTimeout (0) is moved to the return line, and the same thing happens with setTimeout (2000).
- In the return line, operations wait for the call stack to be empty, because only one statement can make time. This is taken care of by the event loop.
- The last console.log () runs, and then the main () appears call stack.
- The event loop detects that the call stack is empty and the call line is empty. So it delivers callbacks (in the first order) so that the call stack is made
npm: -
These are libraries created by an awesome community that will solve most of your common problems. npm (Node package manager) has packages that you can use in your applications to make your development faster and more efficient.
Require:
Demand does three things:
It loads up-and-coming modules collected with Node.js as a file system and HTTP from Node.js v12.0.0 Documents.
Loading third-party libraries such as Express and Mongoose are loaded from npm.
Allows you to customize your files and configure your project in compatible mode.
Requires work, and accepts the “method” parameter and returns the module.exports.
Node Modules:
The Node module is a reusable block of code where its presence does not accidentally affect another code.
You can write your own modules and use them in various applications. Node.js has a set of built-in modules that you can use without any additional installation.
V8 turbo-charge JavaScript using C ++:
The V8 is an open-source engine written in C ++.
JavaScript -> V8 (C ++) -> Machine Code
V8 uses a script called ECMAScript as specified in ECMA-262. ECMAScript was created by Ecma International to personalize JavaScript.
The V8 can work independently or be embedded in any C ++ app. It has hooks that allow you to write your C ++ code that you can make available in JavaScript.
This allows you to add features to JavaScript by inserting a V8 into your C ++ code so that your C ++ code is more accurate than the other ECMAScript standard.
Events:
Something happened to our app that we can respond to. There are two types of events in Node.
System Events: C ++ backbone from a library called libuv. (For example, you have finished reading the file).
Custom Events: JavaScript root.
Writing Hello World at Node.js
We have to do this, don't we?
Make a file app.js and add the following to it.
- console.log("Hello World!");
Open your node terminal, change the directory to the folder where the file is saved and run node app.js.
Yup — you’ve just written Hello World in Node.js.




Comments
Post a Comment