NODE JS

Node is a Javascript runtime built on the Chrome V8 engine

 

It allows us to run JavaScript on the server

Runtime?

Runtime describes software/instructions that are executed while your program is running, especially those instructions that you did not write explicitly, but are necessary for the proper execution of your code.

What are the benefits of Node?

  • Same team can work on the front end and the back end
  • Node is non-blocking. It can do many things at the same time which speaks to its fast speed
  • Can easily migrate code from the front end to the back end
  • Can work with relational of non-relational DB
  • Common data formats (JSON) between the server and client
  • Common software tools exist for the server and client
  • When writing web applications, view templates can be used on both sides
  • Very fast (thanks to V8) and scalable

Best applications for Node

  • REST Api - sometimes you may just have a backend serving  JSON
  • Real Time services ( chat)
  • CRUD Applications - shopping cart, blogs, social
  • building utilities
  • creating microservices

Anything non cpu intensive which would block other things from happening

Node uses V8 Engine

Node is Built using C++

Node had 3 parts:

  1. The libuv library
  2. V8 Engine
  3. Global Interface

The V8 Engine will transpile our code into C++ which will later be transpiled into Machine Language for the computer to read.

libuv

library written in C++ that does that heavy lifting handling the asynchronicity of node making it appear multi-threading and non-blocking as well as turning code into machine language

Global Environment

Browser Api

stores methods on Window object

Node

stores methods on Global

object

Node Project Organization

 

  • Node projects can be made up of 100's of files called modules.
  • Putting code into one file is unreadable and unmanageable
  • We spread our code into these files for organization and readability
  • Files are called modules

Modules Meant to have a Single Responsibility

Single Responsibility Theory Definition:

every module, class or function in a computer program should have responsibility over a single part of that program's functionality, which it should encapsulate.

Encapsulation

the bundling of data, along with the methods that operate on that data, into a single unit. ... Encapsulation can be used to hide both data members and data functions or methods associated with an instantiated class or object

Exports

Object used to transport functions, variables etc to other modules

Let's take a look

module.exports vs exports

Creating a node Project

npm init creates a new node project and a package.json

Creating a package.json

//In your terminal

mdkir <name of directory> & cd <name of directory>

npm init  //creates package.json

//click Enter For Questions

npm install

What is a package.json

  • Holds the metadata of your project
  • Brains of the whole entity
  • List the dependencies needed for your project and their versions
  • Holds specific script definitions
  • Many other things

What is a package.lock.json

Sometimes versions of different packages change and do not work well together. package.lock.json Keeps a record of the versions of the dependencies you use

What is a node_modules

Holds all the dependency code that we use in our project.

Do not change code in this folder

What is npm?

npm is worlds largest software registry

If you want functionality for something

there is probably a package that does it

Dependencies in Node Projects

3 Kinds 

  1. Global
  2. Project
  3. Developer

Node has Native Modules

We require native modules by simply using require and the name of the package

const event = require('events')

Event Emitter

Node runs base on events and many are built into node's base code

We use an Event Emitter on the events object to create custom events

Think of events like you did with the DOM

When you see things like chat apps and/or push notifications, and many other things, they are based on the event emitter

File System

The fs module provides a lot of very useful functionality to access and interact with the file system.

const fs = require('fs')

Lets look at some code

NODE

By JD Richards