Why we need Node.Js

Introduction and Reason behind to use Node Js

Why we need Node.Js

Node js is a tool that every developer will eventually experiment with. so let's get an intro to node js

If you are Looking for a short answer: To make a JavaScript application standalone outside the browser we need an engine outside the browser.

  • What is node js?

    Node.js is an open-source, cross-platform JavaScript runtime environment and library for running web applications outside the client's browser. This environment is built upon “ Chrome’s V8 JavaScript engine”

  • Why is this runtime environment important?

    when Brendan Eich first created JavaScript it was a scripting language which primarily built to run on internet browsers. Using this scripting Language, Javascript websites could only stay inside the browser.

    When JavaScript is executed in the browser, the browser is not the essential player that is compiling your JavaScript. Instead, browsers rely on JavaScript Engines. Chrome Browser uses a V8 engine, Safari Browser uses Nitro, and Mozilla Browser uses Spider Monkey. so the JavaScript applications were existing only in the browser because the engine they depends on was only in browsers.

    If we want a JavaScript application that standalone outside the browser we need an engine outside the browser.

    Node.js is a JavaScript Engine - or you can call it a runtime environment. Node.js allows us to build JavaScript programs that can exist outside of web browsers.

    To install Node Js check out Node Js official website and follow the instructions for your specific operating system. You can also use a package manager like brew (on Mac) or apt-get (on Linux) to install Node.js and npm.

    while understanding Node.js we should also know about npm.

  • Let's understand npm

    Npm stands for Node Package Manager. npm allows developers to develop the scalable application in a given time. If you are wondering what it is I will give you an example of a simple use case.

    while the node is a framework (just a refresher) npm is a tool used to add and remove javascript packages also known as node modules

    • To install npm check out NPM's official website and follow the instructions for your specific operating system. You can also use a package manager like brew (on Mac) or apt-get (on Linux) to install Node.js and npm

Let us assume in a project you need to verify email addresses as if users are entering email in the correct format what you will do is that most probably use regex

const schema = new mongoose.Schema({ 
name:{ 
type: String,
required: true
},
 email: { 
type: String,
 match: /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ } 
});

This is the regex you are going to use for email verification purposes in the schema. On the other hand, If you are using a package named validator in npm you only need to enter this in your schema after importing the package instead of a long regex.

import { isEmail } from 'validator';

const schema = new Schema({
    email: {  
        validate: isEmail
    }
});

Just think how much easier it will be if we are using big packages in complex projects. It will save us a lot of code, time and our life will be easier.

Install node js and npm

npm makes it easy to install and update packages, as well as share your packages with the rest of the npm community and it has a huge library of packages that you can use in your projects, covering a wide range of functionality including email validation, database connectivity, and more.