What is Node.js? Why is everyone using it?

Tram Ho

You are learning JavaScript and may encounter everyone discussing Node.js and have questions like “Isn’t Node.js a web server? Why do I have to install it? Why is everyone using it?” The purpose of this article is to answer those questions.

The features of JavaScript

You need to understand the difference between JavaScript and other programming languages first. JavaScript is a programming language that runs on browsers like Chrome and Firefox, while other popular programming languages like Python and Ruby usually run on computers. It’s like the difference between a browser and a computer – one is for surfing the web, the other is for doing serious work. Or, as I like to say, JavaScript is for browsing the web, while other languages are for browsing the fridge.

image.png

Applications that run on a computer (such as Python or Ruby) can access the functions of the operating system (such as reading and writing files or network communication), which is the difference. Or, in other words, computers are like a box of chocolates – you never know what you’re gonna get!

image.png

Just by accessing a computer, there is a possibility that files on the computer can be read, written, or deleted without permission, so programs that run on browsers (JavaScript) are prevented from accessing the functions of the operating system.

However, it is inconvenient to not be able to access the functions of the OS at all, so the browser mediates limited access to the functions of the OS. Through this mediation, access to the camera and microphone is also enabled, and web conferences are now possible.

image.png

What is Node.js?

Node.js is a tool that allows you to run JavaScript on your computer, instead of just in your browser.

image.png

Node.js installs an application (a JavaScript runtime environment) called “node.exe” when you install it on Windows, just like when you install Python you get “python.exe”. With Node.js, you can access the operating system’s features with JavaScript and do things like read and write files and network communication that you can’t do on a browser.

image.png

Node.js can be used not only for web applications, but also to create other types of applications. For example, you can create command line tools and desktop applications.

Node.js is a server-side environment for running JavaScript?

Node.js is an environment designed to build web servers using JavaScript. That’s why it’s often referred to as a “server-side JavaScript execution environment”. However, nowadays Node.js is also used as an environment to develop “JavaScript to be run in the browser”, so it can be used both as a “server-side JavaScript execution environment” and a “client-side JavaScript development environment”. In other words, Node.js is used for both!

What is npm?

NPM is a handy tool when using Node.js. It’s like Python’s pip, Ruby’s gem (RubyGems), Debian’s apt, Mac’s Homebrew, and Rust’s cargo.

There’s also yarn, but it’s okay because it can do the same thing as npm. “Packages” are libraries and frameworks, like Vue, React, webpack, and jQuery.

If you want to use a library with Node.js, you can install it with npm, so you don’t have to download a js file and write <script src="xxx.js"></script>. It’s like a magical package delivery service!

Why is everyone using Node.js?

Now you can use Node.js, which has enabled you to use JavaScript, to write client-side programs and create web applications and applications for mobile/desktop! Yay!

Purpose 1: I want to write client-side programs in new specifications of JavaScript or TypeScript.

image.png

To use the newest JavaScript specifications (ES2015 and beyond), you need to use Babel to convert it or set up an environment with Node.js.

Let’s not talk about TypeScript this time.

Purpose 2: I want to create a web application.

You can make web applications with Node.js, just like you can with Ruby + Rails or Python + Django.

Execution environmentLanguageWeb framework
RubyRubyRuby on Rails ..vv
PythonPythonDjango …vv
Node.jsJavaScriptExpress …vv

Node.js is a programming language designed to build network applications that can handle large amounts of simultaneous connections. It takes on the role of a web server, receiving HTTP requests and processing them.

You can use Node.js instead of a typical web server (Apache or nginx), but often a configuration with Node.js and Apache or nginx as a reverse proxy is used because Node.js alone is not enough.

People who can only do JavaScript may be told to choose Node.js, but the effort to learn Node.js is not much different from the effort to learn other programming languages, so it’s best to choose without being tied to a language and to research its features well.

Purpose 3: I want to create applications for mobile/desktop.

Using Node.js, you can create mobile applications and desktop applications. For mobile applications, you use the framework React Native, and for desktop applications, you use the framework Electron. Electron is a framework for creating desktop applications using JavaScript, HTML, and CSS.

Electron: https://www.electronjs.org/

image.png

JavaScript is a language used for creating websites, and it’s often used for manipulating the user interface (UI). Because of this, there are many libraries and frameworks available for building UI. This is a big advantage because it allows you to create UI that’s similar to websites.

Additionally, using Electron, you can create applications that handle the features of operating systems like Windows, Mac, Linux, etc. Furthermore, using Electron, you can create applications that run on different platforms such as Windows, Mac, Linux, etc. using the same code.

Desktop applications like Visual Studio Code, Slack, Discord, Twitch, Skype, etc. are created using Electron.

Purpose 4: Others

Node.js is used for various other purposes, like bundling website assets with webpack, converting Sass to CSS using node-sass, using testing tools like Jest and code validation tools like ESLint, setting up a development-friendly web server locally with webpack-dev-server, and building static sites with Gatsby. It’s like a Swiss Army knife for web developers!

Conclusion

Node.js has brought the same level of potential to JavaScript as other programming languages. (The V8 JavaScript engine is also responsible for this, but it was not mentioned in the text because it can make the discussion more complicated.)

As often said, JavaScript does have some weaknesses in its design compared to other languages. Some people may think that it is still on the same level as other languages.

However, JavaScript has a huge user base among young people, and it is evolving at an incredible speed, including its libraries, making it the only programming language with changing trends.

jQuery is now considered a historical relic, but it was born after Rails and Django (it is not easy to compare, but still). It shows how fast things are moving.

Personally, I find it exciting and enjoyable to be able to ride this thrilling wave of evolution in JavaScript that was born in this era.

Finally, I hope that this article will answer common questions such as “Is Node.js not a web server? Why do I have to install it if I just want to write with ES2015?” that are often asked at the beginning.

And Finally

As always, I hope you enjoyed this article and learned something new.
Thank you and see you in the next articles!

If you liked this article, please give me a like and subscribe to support me. Thank you.

Resource

Share the news now

Source : Viblo