Node JS
Node JS
coding.
a non-blocking way.
of domains.
or Asynchronous way.
Synchronous programming
In Synchronous programming, the code execution
happens synchronously. This allows only one task to
execute at a time.
Suppose, we need to read the content of a file and
then database operation is to be executed. When the file
read operation is started, the rest of the code in the
program gets blocked until the file reading operation is
finished. Once the file reading is done, then it continues
to execute the remaining code. Though the database
operation code is not dependent on the file read
operation, it is getting blocked. This kind of code is
considered as blocking code or synchronous code.
Asynchronous programming
The asynchronous code will get executed without
affecting other code execution. This allows multiple
tasks to happen at the same time.
Consider the same scenario of reading a file and then
database operation is to be executed.
On asynchronously implementing this, when the file
read operation is started, it will not wait for the read
operation to complete, it will just continue execution of
the rest of the code.
Once the file reading is done, it will be informed and
the corresponding function gets called. This provides a
non-blocking way of executing the code.
This improves system efficiency and throughput.
In Node.js, asynchronous programming is implemented using
the callback functions.
Callback function: A callback is a function passed as an
argument to another function and it will be executed after the
task gets completed. It helps in non-blocking code execution.
setTimeout(() => {
console.log("after 20 seconds");
}, 20000);
setTimeout() takes two arguments. The first argument is the
callback function and the second argument is the delay in
milliseconds. The callback function is called after 20 seconds.
In Node.js, at the completion of each task, the
respective callbacks written gets invoked. This makes
Node.js highly scalable as it can handle a large number of
requests in a non-blocking way.
4. Single-threaded event loop model
node -v
The flag -v will display the version of Node.js installed in the
machine.
Demo steps:
locally.
Best Practice Tip: Start all projects with npm init. This will
following command.
npm update -g <package_name>
How to uninstall the packages in our application?
We can uninstall the package or module, which we
NPM Alternatives
module.
The property main represents the entry point of the
application.
The test represents all the test scripts to run.
};
};
nodemon app.js
Thus the 'nodemon' starts the application in watch
Highlights:
Demosteps:
Step 1: Create a file log.js and paste the below code in the file.
If the file does not exist, then it creates a new file with the
content, else it appends the given content to the existing file.
const fs = require('fs');
const { promisify } = require('util');
const appendFile = promisify(fs.appendFile);
(async () => {
try {
await appendFile('myLogger.txt', `Request received @ $
{Date()} \n`);
console.log('File content appended successfully');
}
catch (err) { console.log(err);
}
})();
Step 2: Run the above code using node command 'node
log.js' to see the output.