Reading time: 20 minutes
In this article, we will take you through the basic idea of NodeJS and various ideas such as NodeJS's package manager NPM, Installation guide and a basic example along with When to use NodeJS? and when not to use?.
⭐ Introduction to Node.js
Now, what does that actually mean? 😕
Well, it turns out that we actually can.🎉
💫 And this is absolutely fantastic and game-changing for web development.
⭐ Pros of using Node.js :
Now let’s see some Pros of using Node.js
- Single threaded based on event driven, non-blocking I/O model.
- Perfect for building fast and scalable data-intensive applications.
- Companies like Netflix, Uber, Ebay and Paypal have started using it in production.
- Another great reason for using Node.js is the presence of huge library of open-source packages, or modules, available for everyone for free.It is called npm and you’ve probably used it.
- Very active developer community that are constantly improving Node.js and helping beginners like us.
⭐ Where to use Node.js :
We know that Node.js is perfect for building fast and scalable data-intensive applications, which makes it a perfect fir for building applications like
- Building an API with database behind it, and preferably a NoSql database like MongoDb.
- Data-streaming applications (like Youtube or Netflix).
- Real-time chat applications.
- Server-side web applications.
⭐ Where not to use Node.js :
Great, we are going well, but we should also be aware of where to not use Node.js.
There are some apps that we should not build with Node.js. And this is when our app needs some super heavy server-side processing like having:
- image manipulations
- video conversion
- file compression or anything like that.
So, in this case, we're better off using something like Ruby on Rails, PHP, or Python. Because NodeJS just wasn't developed for these use cases and although there are solutions for dealing with them, we should always use the right tool for the task at hand.
⭐ Node Package Manager (npm) -
And the foundation of all these tools is the Node Js and NPM ecosystem.
In this ecosystem, we can find all kinds of third party open source tools and libraries and frameworks needed for modern web development.
Now, in order to use and share these packages, we need some kind of tool to install and manage them. And that's where the node package manager, or NPM, comes in.
So, npm is just a simple command line interface that allows us to install and manage third party open source tools, libraries and frameworks needed for modern and real life web development.
In addition, NPM also allows us to write scripts to use our development tools.
⭐ Installation of Node.js and npm -
For Ubuntu, you can install Node.js using the following steps:
sudo apt-get install curl python-software-properties curl -sL https://deb.nodesource.com/setup_12.0 | sudo -E bash - sudo apt-get install nodejs
- For Windows, Download and install the most recommended version of node.js from here..
For other system configurations, download and install the binary files from here.
- After the installation is completed, open the terminal to confirm if node.js and npm have been installed correctly.Node package manager comes with node itself.
node -v npm -v
If there’s an error, this means there’s some error in the installation process.
⭐ Modules in Node.js
No further installation is necessary for using these modules. Some of the modules are -
http- http is a built-in module in Node.js and is used to transfer data over the Hypertext Transfer Protocol (HTTP).It is also used to create web servers that listens to requests and send back responses.
fs - fs stands for File System. This modules enables us to work with file system on our computer. It lets us create,update,delete and update files of our system.
Including modules -
To include any of the module, we can use the require() method:
const http = require('http');
You can read more about Node.js modules and their usage here.
⭐ Hello World in Node.js
Ofcourse, we had to this, don't we ?
So, Create file index.js and add the following code to it.
Open your node terminal/command line interface, change the directory to the folder where the file is saved and run
You will see "Hello World" printed on your screen.
Hurray ! 🎉
You just wrote Hello World in Node.js.