overview of node js

50
JACOB NELSON SOFTWARE ARCHITECT

Upload: jacob-nelson

Post on 23-Jan-2018

218 views

Category:

Technology


0 download

TRANSCRIPT

JACOB NELSONSOFTWARE ARCHITECT

Node.js is not a silver-bullet new platform that will dominate the web development world. Instead, it’s a platform that fills a particular need.

A word of warning

There are some really excellent JavaScript people out there. I'm not one of them.

Node JS is a powerful tool for controlling web servers, building applications, and creating event-driven programming and it brings JavaScript, a language familiar to all web developers, into an environment independent of web browsers.

Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.

Runtime is when a program is running (or being executable). That is, when you start a program running in a computer, it is runtime for that program.

Node.js is not a JavaScript framework

Node.js allows you to run JavaScript code in the backend, outside a browser.

+

Node.js ships with a lot of useful modules, so you don't have to write everything from scratch

Thus, Node.js is really two things: a runtime environment and a library.

Requirements

▪ JavaScript

▪ Command-Line Tool

V8

The V8 JavaScript Engine is an open source JavaScript engine developed by The Chromium Project for the Google Chrome web browser. It has seen used in many other projects, such as Couchbase, MongoDB and Node.js.

https://en.wikipedia.org/wiki/V8_(JavaScript_engine)

Threading

Node is single-threaded and uses a concurrency model based on an event loop. It is non-blocking, so it doesn't make the program wait, but instead it registers a callback and lets the program continue. This means it can handle concurrent operations without multiple threads of execution, so it can scale pretty well.

Installing Node

If you're using OS X or Windows, the best way to install Node.js is to use one of the installers from the Node.js download page.

If you're using Linux, you can use the installer, or you can check Node Source's binary distributions to see whether or not there's a more recent version that works with your system.

Test

node -v

npm, the official Node package manager

npm is the package manager for JavaScript.

npm makes it easy for JavaScript developers to share and reuse code, and it makes it easy to update the code that you're sharing.

npm, the official Node package manager

Node comes with npm installed so you should have a version of npm. However, npm gets updated more frequently than Node does, so you'll want to make sure it's the latest version.

npm, the official Node package manager

▪ It installs application dependencies locally, not globally.

▪ It handles multiple versions of the same module at the same time.

▪ You can specify tarballs or git repositories as dependencies.

▪ It's really easy to publish your own module to the npmregistry.

▪ It's useful for creating CLI utilities that others can install (with npm) and use right away.

Npm behind firewall

npm config set proxy http://gateway.zscaler.net:9400

npm config set https-proxy http://gateway.zscaler.net:9400

Updating npm

npm install npm@latest -g

Test

npm -v

Your First Program

console.log('Hello World');

npm init

The npm init command is a step-by-step tool to scaffold out your project. It will prompt you for input for a few aspects of the project in the following order:

npm init

▪ The project's name

▪ The project's initial version

▪ The project's description

▪ The project's entry point (meaning the project's main file)

▪ The project's test command (to trigger testing with something like Standard)

▪ The project's git repository (where the project source can be found)

▪ The project's keywords (basically, tags related to the project)

▪ The project's license (this defaults to ISC - most open-source Node.js projects are MIT)

npm init

Once you run through the npm init steps above, a package.json file will be generated and placed in the current directory. If you run it in a directory that's not exclusively for your project, don't worry! Generating a package.json doesn't really do anything, other than create a package.json file. You can either move the package.jsonfile to a directory that's dedicated to your project, or you can create an entirely new one in such a directory.

npm init

Once you run through the npm init steps above, a package.json file will be generated and placed in the current directory. If you run it in a directory that's not exclusively for your project, don't worry! Generating a package.json doesn't really do anything, other than create a package.json file. You can either move the package.jsonfile to a directory that's dedicated to your project, or you can create an entirely new one in such a directory.

npm init options

If you invoke it with -f, --force, -y, or --yes, it will use only defaults and not prompt you for any options.

package.json

package.json file can be described as a manifest of your project that includes the packages and applications it depends on, information about its unique source control, and specific metadata like the project's name, description, and author.

package.json

package.json

package.json also contains a collection of any given project's dependencies. These dependencies are the modules that the project relies on to function properly.

package.json - dependencies

Dependencies are specified in a simple object that maps a package name to a version range. The version range is a string which has one or more space-separated descriptors.

Having dependencies in your project's package.json allows the project to install the versions of the modules it depends on. By running an install command inside of a project, you can install all of the dependencies that are listed in the project's package.json - meaning they don't have to be (and almost never should be) bundled with the project itself.

package.json - devDependencies

It also allows the separation of dependencies that are needed for production and dependencies that are needed for development. In production, you're likely not going to need a tool to watch your CSS files for changes and refresh the app when they change.

package.json – module version

▪ version Must match version exactly

▪ >version Must be greater than version

▪ >=version etc

▪ <version

▪ <=version~version "Approximately equivalent to version"

▪ ^version "Compatible with version"

▪ 1.2.x 1.2.0, 1.2.1, etc., but not 1.3.0

package.json – module version

▪ http://...

▪ * Matches any version

▪ "" (just an empty string) Same as *

▪ version1 - version2 Same as >=version1 <=version2.

▪ range1 || range2 Passes if either range1 or range2 are satisfied.

▪ git...

▪ user/repo See 'GitHub URLs' below

▪ tag A specific version tagged and published as tag

▪ path/path/path

Modules - Installation

npm install <-g> module name

Modules – Installation

npm install

Once you run this, npm will begin the installation process of all of the current project's dependencies.

Modules – Installation Options

npm install <module> --save

when installing a module: --save, it will install and save it as an entry in the dependencies

npm install <module> --save-dev

when installing a module: --save-dev, it will install and save it as an entry in the devDependencies

Modules – Listing

Sometimes is useful to see the list of packages that you have installed on your system. You can do that with the following commands:

# list all installed modules with dependencies

npm ls

# list all installed modules without dependencies

npm ls --depth=0

# list all installed globally dependencies

npm ls -g --depth=0

Extraneous Modules

Modules which are installed and are found on node_modules folder, but not included as Dependency or devDependency in package.json

Module – uninstallation

# uninstall package and leave it listed as dep

npm uninstall <package_name>

# uninstall and remove from dependencies

npm uninstall --save <package_name>

# uninstall global package

npm uninstall -g <package_name>

# remove uninstalled packages from node_modules

npm prune

Modules – view all versions of an NPM Package

The easy way to view all released versions of an npm package is to use the following command

npm show <module>@* version

Modules - Importing

▪ Java or Python use the import function to load other libraries,

▪ PHP and Ruby use require.

▪ In Node you can load other dependencies using the require keyword.

Modules - Importing

▪ For example, we can require some core modules:

▪ var http = require('http');

▪ var dns = require('dns');

Modules - Importing

What node.js will do in this case, is to first look if there is a core module named http, and since that's the case, return that directly. But what about non-core modules, such as 'mysql'?

In this case node.js will walk up the directory tree, moving through each parent directory in turn, checking in each to see if there is a folder called 'node_modules'. If such a folder is found, node.js will look into this folder for a file called 'mysql.js'. If no matching file is found and the directory root '/' is reached, node.js will give up and throw an exception.

Modules - Importing

▪ We can also require relative files:

▪ var myFile = require('./myFile'); // loads myFile.js

callbacks

In asynchronous programming we do not return values when our functions are done, but instead we use the continuation-passing style (CPS).

With this style, an asynchronous function invokes a callback (a function usually passed as the last argument) to continue the program once the it has finished.

When Not to Use?

▪ CPU heavy apps

▪ Simple CRUD / HTML apps

▪ NoSQL + Node.js

When to Use?

▪ JSON APIs

▪ Single page apps

▪ Streaming Data

▪ Soft Real time Applications

▪ https://www.guru99.com/node-js-tutorial.html

▪ http://www.tutorialsteacher.com/nodejs/nodejs-basics

▪ https://www.javatpoint.com/what-is-nodejs

▪ http://adrianmejia.com/blog/2016/08/19/Node-Package-Manager-NPM-Tutorial/

▪ https://nodesource.com/blog/an-absolute-beginners-guide-to-using-npm/

▪ Best Practices

▪ https://www.codementor.io/mattgoldspink/nodejs-best-practices-du1086jja