What is Node?

Before you learn about Node.js you need to learn about node. You can run JavaScript code “Outside the web browser” in the Node environment. “Hey everyone, give me your JS code and I’ll run it,” said Node. The JavaScript code is converted to Machine Code using Google’s V8 Engine.

Node lacks access to some capabilities that are only available in web browsers, such as the DOM, the window object, or even the localStorage, because Node executes JavaScript code outside of the browser.

This means that at any point in your code, you can’t type in document.querySelector() or alert() as these will produce errors (This is what is shown in the below image).

Remember: Node is meant for server-side programming, while those browser features are meant for client-side programming. Learn about How to learn programming language

How to install Node.js

How you can install Node.js on your system: a package manager, the official website installer or nvm

Node.js can be installed in different ways. This post highlights the most common and convenient ones. Official packages for all the major platforms are available at https://nodejs.dev/download/.

One very convenient way to install Node.js is through a package manager. In this case, every operating system has its own. Other package managers for MacOS, Linux, and Windows are listed in https://nodejs.dev/download/package-manager/

nvm is a popular way to run Node.js. It allows you to easily switch the Node.js version, and install new versions to try and easily rollback if something breaks. It is also very useful to test your code with old Node.js versions.

See https://github.com/nvm-sh/nvm for more information about this option.

In any case, when Node.js is installed you’ll have access to the node executable program in the command line.

Global Variables in node.js

Let’s begin this tutorial by learning about some variables called Global Variables that are present in NodeJS. These are essentially variables that you can access from wherever in your code, regardless of how deeply nested it is, and which store some data.

You should know about these commonly used Global variables:

  • __dirname: This variable stores the path to the current working directory.
  • __filename: This variable stores the path to the current working file.

Let’s put them to use and assess their value. To do this, create a new folder on your desktop named NodeJSTut and open it in your preferred text editor (for the duration of the lesson, we’ll be using VS Code). Open a new integrated VS Code Terminal and create a new file called app.js.

Paste the following code in the app.js file and save it:

// __dirname Global Variable
console.log(__dirname);

// __filename Global Variable
console.log(__filename);

To run this code using Node, type in the following command in the terminal and press Enter: node app.js. You will see the absolute path to the present working directory and the path to the current file is printed in the terminal. This is what the output looks like in my case:

C:\Desktop\NodeJSTut
C:\Desktop\NodeJSTut\app.js

You can go ahead and create your own global variables which can be accessed from anywhere in your code. You can do so, like this:

// Define a global variable in NodeJS
global.myVariable = 'Hello World';

// Access the global variable
console.log(myVariable); // Output: Hello World

Modules in Node.js

Modules are encapsulated code blocks that communicate with an external application. These can be a single file or a collection of multiple files/folders. These are reusable, hence they are widely used. Let us learn about Nodejs Modules.

Types of Modules in Nodejs:

There are three types of modules
1) Core Modules
2) local Modules
3) Third-party Modules

What are HTTP Methods?

Actions that a Client can carry out on a Server are referred to as HTTP methods, often known as HTTP verbs. There are 4 HTTP Methods.

  • GET: Retrieves a resource from the server
  • POST: Inserts a resource in the server
  • PUT: Updates an existing resource in the server
  • DELETE: Deletes a resource from the server

What is a Status Code?

HTTP status codes are three-digit numbers that indicate the status of a HTTP request made to a server. They are server responses that provide information about the request’s outcome. Here are some of the most common HTTP status codes and what they represent:

Conclusion

With this we come to the end of this tutorial – I hope you liked it and learned a lot about Node.

Do share your learnings from this guide on Twitter and LinkedIn (#LearnInPublic) and follow freeCodeCamp for more such informative coding articles.

Connect with me on Twitter: Twitter – Krish4856, DMs Open.

See you next time


Leave a Reply

Your email address will not be published. Required fields are marked *