Skip to content

Latest commit

 

History

History
64 lines (64 loc) · 7.09 KB

Introduction_to_NodeJS.md

File metadata and controls

64 lines (64 loc) · 7.09 KB

Lets start by listing the topics we are going to learn 👇

What is Node.js --> Overview of NPM --> Commands to use NPM --> Packages --> Local modules + Modules.export
                                                                 / \
                                                             Local  Global

Web-Development is an emerging field with a lot of new peaks. It can be divided into two parts:👉 Front-end development and Back-end development. Front-end development basically means to describe the outer view of user interface. If we see in terms of cars -> front-end means the structure, the colour,everything that a user can see. Back-end development means developing the working, the connectivity of user interface. In other words -> back-end of cars means how the engine works, the various machinery involved in its working, everything that a user cannot see. Back-end is like a working of engine under the bonnet which usually driver doesn't know how that works.

I am here to elaborate the technology used for back-end development: Node-js. The first question arises :What is Node.js ❓ "Node-js is a javascript runtime environment that executes javascript code outside web browser." This can be termed as a technical definition of Node.js. Lets understand it in simpler words: basically node-js is a runtime environment that allows us to write and implement javascript code for server side programming. It is very efficient as it is capable of using Javascript for front-end as well as for back-end. No separate language is required for back-end. If you are familiar with Javascript then it becomes very easy to learn Node.js.

Switching on to Npm 👇

npm -> Node Package Manager Node.js is popular as it provides various features. One of them is npm. npm is a standard package manager for node.js, the package manager makes it easier for programmers to publish and share source code of Node.js packages. It is collection of different modules, that makes everything quite easy. It all comes when you will install Node.js. These modules are basically solutions or source code that can be used by anyone directly in their application. Example: if we want to connect node.js to our database, there are 2 ways: ➡️ either we can write the entire code from scratch ➡️ or we can use the package/module that already comprises of the pre-written source code So this is a really good feature as we can use various dependencies / modules directly by using "require" keyword. NPM is a command line tool that installs, updates or uninstalls Node.js packages in your application. You can fetch the packages, install them and use in your project.

Heading over to how to use npm 👇

You can go to the official site of npm and search whatever package you want to use. ⭐Link to npm site https://www.npmjs.com/, after installing node.js, you will get npm as well. When you are inside your project directory :

❗ Use the following commands to get started with node.js and to use npm packages ❗

  1. Head over to the terminal
  2. Move into your project directory
  3. Use "npm init" command 👉This command will generate the package.json file inside your project directory.This file comprises of dependencies and version control of them in your project
  4. You can change or set to default by simply pressing enter key until everything gets started and then you can move further
  5. Express is the most used and the most famous framework, it can be used by installing "express" package
  6. Command used for installing any package->"npm install package_name" Example: for express, we need to write "npm install express" command in terminal.

⭐This will install latest version of the respective package. Inside your directory, there will be one more file "package-lock.json" -> this will show all the modules along with their versions and dependencies that comes along. For example-> when you install "express" package, it itself comes with various modules and dependencies, so that can be viewed inside "package-lock.json" file.

Packages can be installed :1️⃣ Locally 2️⃣ Globally

Locally👉 When you are in your project directory and install the packages using above commands, packages are installed locally in your current directory. "npm install package_name" this command installs the package in your current directory and the package gets installed inside "node_modules" folder in the project directory. ⭐ Each time you make a new project, you have to install the packages using the above commands. After installing packages can be used using "require" keyword.

Globally👉 To install the packages globally, use the command 👉 "npm install -g package_name" ⭐ This installs the package globally so that all node.js applications on the device can import and use the installed packages.NPM installs global packages into //local/lib/node_modules folder.

❗Packages can be updated and uninstalled using the commands : "npm update package_name" and "npm uninstall package_name". Update command will update the package to the latest version and uninstall means local package is removed from the directory.

Local Modules 😀

These modules are created locally in your node.js application, then save it in a file and you can reuse it by including it into other files using "require" keyword. 😁You can even package it and distribute it via NPM, so that Node.js community can use it. ❗Example: In this code, I have uploaded a local module file named as "app.js" file. This is a calculator code module❗

☝️ this code shows a module of a calculator that gives output based on function we require

Also uploaded the code of our main file "sample.js" in which we "require" calculator local module.

☝️ here we have required the local module and called the "add" function. Now lets see the output on our terminal:

⭐Module.exports

We have used "module.exports" in above code also. Module.exports is basically a special object that is used to expose a function, object, variable as a module in node.js. In above code, module.exports exposes calculator object as a module. We can use "module.exports" or "exports".

⚡Here is the special code of MySQL connectivity in Node.js, in which "database.js" file is used as a local module.

Code file-> https://github.com/Sugandha-999/Node-js/blob/main/database.js