Day 1 of 100days of code to Node JS
Fundamentals of Node JS 🌐
Fundamentals of Node JS 🌐
Ever wondered: what is Node JS and why is it necessary? What are its advantages? Under what circumstances should we or should we not use Node JS?
What is Node JS? 🤔
Node JS is a JavaScript runtime, constructed on Google’s open source V8 engine. Each browser has its own JavaScript engine, responsible for executing JavaScript code. Be it Firefox using Spider Monkey, Microsoft Edge with Chakra, or Google Chrome with the V8 engine.
Before 2009, it was impossible to run a JavaScript program outside of a browser. However, that changed when Ryan Dahl, our Node JS creator, ingeniously decided to execute JavaScript programs outside of the browser. To make this possible, he utilized the fastest JavaScript V8 engine and integrated it into a C++ program. Voila! Here comes Node JS.
Advantages of using Node JS 😃
Node JS comes with some spectacular unique APIs, different from ones you’d find in a browser. This API framework provides an environment for JavaScript code to run outside of the browser. This newfound ability expanded the boundaries — we can access the file system, improve our network capabilities and create a perfect condition for using Node JS as a server-side language. Today, we can use JavaScript on the server-side of web development, to build fast and highly scalable network applications.
Where should we use and not use Node JS? 🧐
Node JS is perfect for creating fast and scalable data-intensive applications, making it highly versatile. You can construct an API with a backend database using Node JS. However, Node JS may not be suitable for heavy server-side processing apps, such as heavy image manipulations, video conversions, or file compressions. While there are solutions for these tasks, it’s always better to use the right tools for the job. For such apps, technologies like PHP or Python might be more appropriate.
Node JS’s greatest strength lies with JavaScript as its programming language. This allows us to use the same technology for both front-end and back-end development. Simplifying by not introducing a new programming language for building your backend application, saving the need to switch between languages. An added advantage is the abundant availability of open source packages in NPM, making the development process faster and more efficient.
Installing Node JS ⚙️
You can download the LTS(Long Term Support) version Node JS from node.js.org, making sure to choose the installer based on your operating system. Remember, the LTS version is more stable and less subject to issues with new updates as opposed to the current version, which updates frequently.
How to run JS outside a browser?
After installing Node JS, open the terminal and run: node -v to check the installed version of Node JS on your machine. If you’re not seeing a version number or an error occurs, it signifies that Node JS was not properly downloaded or installed. A simple reinstall should fix it.
Node JS Project 🎯
Node JS allows reading inputs and outputs in the terminal. You can execute a Node JS file or output commands in the terminal using the console.log method by running: node filename, e.g., node app.js in terminal.
Node JS provides a module for reading inputs from the user in the terminal. To import this module, we use require function, like require(“readline”). This module gives us an interface to read inputs from and write inputs to the terminal.
To read a file, Node JS provides a method, question(). This method waits for the user to input a value before executing a callback function, which processes the input. To close the interface after execution, we use rl.close().
Node JS emits a close event when the interface is closed. We can listen to this event and execute logic when it happens using rl.on(), finally using process.exit(0) to exit the event.
Next on the agenda: Working with Files in Node JS 📑
Thank you for tagging along on this brief exploration of Node JS. Have a fantastic day ahead! 🚀🎉