📕
Dan Fitz's Notes
  • README
  • Ai
    • Supervised Machine Learning
      • Introduction To Machine Learning
      • Regression With Multiple Input Variables
      • Classification
  • Csharp
    • C Sharp Advanced
      • Generics
      • Delegates
      • Lambda Expressions
      • Events
    • C Sharp Fundamentals
      • Intro To C
      • Primitive Types And Expressions
      • Non Primitive Types
      • Control Flow
      • Arrays And Lists
      • Working With Dates
      • Working With Text
      • Working With Files
      • Debugging Applications
    • C Sharp Intermediate
      • Classes
      • Association Between Classes
      • Inheritance
      • Polymorphism
      • Interfaces
  • Java
    • Inheritance Data Structures Java
      • Inheritance Polymorphism Using Overriding And Access Modifiers
      • Abstract Classes And Debugging
      • File I O And Exceptions
      • Collections Maps And Regular Expressions
    • Intro To Java
      • Introduction To Java Classes And Eclipse
      • Unit Testing Arrays And Array Lists
      • Static Variables Methods And Polymorphism Using Overloading
  • Javascript
    • Algorithms Data Structures
      • Big O Notation
      • Analyzing Performance Of Arrays And Objects
      • Problem Solving Approach
      • Problem Solving Patterns
      • Recursion
      • Searching Algorithms
      • Bubble Selection And Insertion Sort
      • Merge Sort
      • Quick Sort
      • Radix Sort
      • Data Structures Introduction
      • Singly Linked Lists
      • Doubly Linked Lists
      • Stacks And Queues
      • Binary Search Trees
      • Tree Traversal
      • Binary Heaps
    • Complete Nodejs
      • Understanding Node.js
      • REST AP Is And Mongoose
      • API Authentication And Security
      • Node.js Module System
      • File System And Command Line Args
      • Debugging Node.js
      • Asynchronous Node.js
      • Web Servers
      • Accessing API From Browser
      • Application Deployment
      • Mongo DB And Promises
    • Complete React Native
      • Working With Content
      • Building Lists
      • Navigating Users Between Screens
      • State Management
      • Handling Screen Layout
      • Setting Up An App
      • More On Navigation
      • Advanced Statement Management With Context
      • Building A Custom Express API
      • In App Authentication
    • Epic React
      • React Fundamentals
      • React Hooks
      • Advanced React Hooks
      • Advanced React Patterns
      • React Performance
    • Fireship Firestore
      • Firestore Queries And Data Modeling Course
      • Model Relational Data In Firestore No SQL
    • Functional Light Javascript
      • Intro
      • Function Purity
      • Argument Adapters
      • Point Free
      • Closure
      • Composition
      • Immutability
      • Recursion
      • List Operations
      • Transduction
      • Data Structure Operations
      • Async
    • Js Weird Parts
      • Execution Contexts And Lexical Environments
      • Types And Operators
      • Objects And Functions
      • Object Oriented Java Script And Prototypal Inheritance
      • Defining Objects
    • Mastering Chrome Dev Tools
      • Introduction
      • Editing
      • Debugging
      • Networking
      • Auditing
      • Node.js Profiling
      • Performance Monitoring
      • Image Performance
      • Memory
    • React Complete Guide
      • What Is React
      • React Basics
      • Rendering Lists And Conditionals
      • Styling React Components
      • Debugging React Apps
      • Component Deep Dive
      • Building A React App
      • Reaching Out To The Web
      • Routing
    • React Testing
      • Intro To Jest Enzyme And TDD
      • Basic Testing
      • Redux Testing
      • Redux Thunk Testing
    • Serverless Bootcamp
      • Introduction
      • Auction Service Setup
      • Auction Service CRUD Operations
      • Auction Service Processing Auctions
    • Testing Javascript
      • Fundamentals Of Testing
      • Static Analysis Testing
      • Mocking Fundamentals
      • Configuring Jest
      • Test React Components With Jest And React Testing Library
    • Typescript Developers Guide
      • Getting Started With Type Script
      • What Is A Type System
      • Type Annotations In Action
      • Annotations With Functions And Objects
      • Mastering Typed Arrays
      • Tuples In Type Script
      • The All Important Interface
      • Building Functionality With Classes
    • Web Performance With Webpack
      • Intro
      • Code Splitting
      • Module Methods Magic Comments
  • Other
    • Algo Expert
      • Defining Data Structures And Complexity Analysis
      • Memory
      • Big O Notation
      • Logarithm
      • Arrays
      • Linked Lists
      • Hash Tables
      • Stacks And Queues
      • Strings
      • Graphs
      • Trees
    • Aws Solutions Architect
      • AWS Fundamentals IAM EC 2
    • Fundamentals Math
      • Numbers And Negative Numbers
      • Factors And Multiples
      • Fractions
    • Mysql Bootcamp
      • Overview And Installation
      • Creating Databases And Tables
      • Inserting Data
      • CRUD Commands
      • The World Of String Functions
      • Refining Our Selections
      • The Magic Of Aggregate Functions
    • Random Notes
      • Understanding React Hooks
  • Python
    • Data Analysis Using Python
      • Loading Querying And Filtering Data Using The Csv Module
      • Loading Querying Joining And Filtering Data Using Pandas
      • Summarizing And Visualizing Data
    • Intro To Python
      • Course Introduction Intro To Programming And The Python Language Variables Conditionals Jupyter Notebook And IDLE
      • Intro To Lists Loops And Functions
      • More With Lists Strings Tuples Sets And Py Charm
      • Dictionaries And Files
Powered by GitBook
On this page
  • What is Node.js?
  • How is it possible to run JavaScript server-side?
  • Differences between browser and Node.js
  • Why Should I Use Node.js?
  • Event-driven non-blocking I/O model
  • npm
  1. Javascript
  2. Complete Nodejs

Understanding Node.js

PreviousComplete NodejsNextREST AP Is And Mongoose

Last updated 3 years ago

What is Node.js?

Originally, JavaScript could only be run inside the browser, limiting what it could do.

With Node.js, JavaScript could be used server-side, allowing devs to create:

  • Web servers

  • Command line interfaces

  • Application backends

  • And more

How is it possible to run JavaScript server-side?

According to the , Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. Let's break that down.

A JavaScript engine takes in JavaScript code and compiles it down to machine-readable code. Chrome's V8 JavaScript engine does this for Chrome browsers. All browsers each have their own engine. Node.js leverages the V8 JavaScript engine to perform compilation.

Note: The V8 engine is written in C++. So is Chrome and Node.js!

Node.js is a JavaScript runtime: something that provides tools and libraries specific to the Node.js environment. In the browser, its runtime includes things like the window object and document object (for DOM manipulation). In Node.js, these tools and libraries include the fs (filesystem) and os objects. They allow you to do things like:

  • Set up web servers

  • Integrate with file systems for reading and writing

These special tools in Chrome and Node.js are written in C++. They're what allow you to manipulate the DOM or file systems (respectively), even though these features aren't technically built into JavaScript. They are called bindings. For example, document.querySelector in Chrome is binded to a C++ function written in Chrome. So is fs.readFile in Node.js.

In contrast, things like the String and Number prototypes are provided directly by the V8 engine itself, not Node.js or the browser. They're what allow you to do things like "Dan Fitz".length or 2 + 3.

Differences between browser and Node.js

Node.js comes with these objects and more:

  • global is closest to window in the browser: it provides the initial execution context with tons of built-in properties and methods.

  • process is closest to document: it gives us the ability to manipulate the Node process running.

    • Example: process.exit() exits the Node.js REPL.

Why Should I Use Node.js?

Furthering the description of Node.js on their site:

Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js package ecosystem, npm, is the largest ecosystem of open source libraries in the world.

Event-driven non-blocking I/O model

When Node.js reads data from a file or queries data from a database, it's performing an I/O operation. It's sending a request or input and receives data or an output back.

The nature of I/O is that it takes time to perform. That's why Node.js is non-blocking: while waiting for a response, it can continue to run code.

Pro tip: An extra feature of non-blocking runtimes like Node.js is that they overlap I/O operations. If I send 2 database queries, they can run concurrently in the background.

These 2 database queries are often given callbacks, which execute when the data is returned. Just like the event queue in the browser, that's why Node.js is event-driven.

Example:

// The callback runs when the query is complete
getUser(1, user => console.log(user));

npm

Node.js makes npm possible. (I suppose because of its ability to create CLIs and its ability to read and write files in file systems.) Because npm is such a huge package manager, it's an incredible resource that can solve almost anything.

Pro tip: You can access Node.js API bindings in their .

Node.js website
docs