What is NodeJS?
• JavaScript Runtime: [Link] is an open-source, cross-platform runtime environment
for executing JavaScript code outside of a browser.
• NodeJs is a JavaScript in a different environment means Running JS on the server or
any computer.
• Built on Chrome's V8 Engine: It runs on the V8 engine, which compiles JavaScript
directly to native machine code, enhancing performance.
• V8 is written in C++ for speed.
• V8 + Backend Features = NodeJs
NodeJs Features
• File System Access: Provides APIs to read and write files directly, which is not
possible in browser environments for security reasons.
• Server-Side Capabilities: [Link] enables JavaScript to run on the server, handling
HTTP requests, file operations, and other server-side functionalities.
• Modules: Organize code into reusable modules using require().
• Window Object: The global window object, which is part of web browsers, is absent
in [Link].
• DOM Manipulation: [Link] does not have a built-in Document Object Model
(DOM), as it is not intended to interact with a webpage's content.
• BOM (Browser Object Model): No direct interaction with things like navigator or
screen which are part of BOM in browsers.
Modularity in [Link]
Modularity is a key concept in [Link] that allows developers to break down their code into
smaller, reusable components. This enhances maintainability, readability, and collaboration.
Key Concepts
1. Modules:
• A module is a self-contained piece of code that can be reused across different
parts of an application.
• [Link] has a built-in module system that allows you to create and use
modules easily.
2. CommonJS Module System:
• [Link] uses the CommonJS module system, which defines how modules are
structured and how they interact.
• Each file in [Link] is treated as a separate module.
3. Exporting Modules:
• To make functions, objects, or variables available to other modules, you
use ‘[Link]’ or ‘exports’.
// [Link]
const myFunction = () => {
[Link]("Hello from myFunction!");
};
[Link] = myFunction;
4. Importing Modules:
• To use a module in another file, you use the require function.
// [Link]
const myFunction = require('./myModule');
myFunction(); // Outputs: Hello from myFunction!
5. Built-in Modules:
• [Link] comes with several built-in modules (e.g., fs for file system
operations, http for creating web servers, etc.).
• You can import these modules using require.
const fs = require('fs');
6. Third-Party Modules:
• You can also use third-party modules from the npm (Node Package Manager)
registry.
• To install a package, use the command: npm install package-name.
• After installation, you can require it in your code.
const express = require('express');