SlideShare a Scribd company logo
1 of 41
Header
Microsoft Virtual Academy
Introduction to Node.js
Eric W. Greene
Produced by
Mastering Node.js, Part 1
Course Overview
 What is Node.js?
 Installing Node.js
 Node REPL
 Node Programs
 Managing Packages with NPM
 Building a Simple Web Server
 Reading Files from Disk
 Debugging
 Creating a Package
 Use NPM to run a package
What is Node.js?
 Node.js is a cross-platform, JavaScript environment on the server
 JavaScript is a glue language for C++ modules
 It is powered by Google’s V8 JavaScript engine
 Node.js uses an event driven, non-blocking I/O model
 Node.js is like a web browser with a different set of C++ modules
 It provides modules for interacting with local system resources such as
processes, file system, networking, etc…
Node.js compared to a Web Browser
JavaScript
V8
DOM XHR
Audio
File Reader
Timers
Web
Sockets
Video
Many
More…
JavaScript
V8
File
System
Net
Cluster
HTTP
Timer
Process
Stream
Many
More…
Web Browser Node.js
Installing Node.js
 Node.js can be installed from a platform specific installer, precompiled
source code, and compiled from source code
 To download the installer: http://www.nodejs.org
 When installing using the platform specific installer, most users accept
the defaults
Node.js Versions
 The installer and pre-compiled binaries can be downloaded for either
the Long-Term Support version or Current Version
 Long-Term Support version is best for applications in production
 The Current Version is best for working with the newest features
Installing Node.js
Node REPL Environment
 REPL stands for Read, Evaluate, Print, Loop
 Allows the user to enter a JavaScript command, evaluate it, print the
results, and loop to enter another command
 Supports multiline commands, customize command prompt and
preconfigured context
 Sessions can be saved, and reloaded again
 Useful for learning JavaScript or executing tasks from a JavaScript
command line
Using the Node REPL Environment
Node.js Programs
 Node.js programs can be created using JavaScript files which are
executed with the Node.js executable
 JavaScript files contain JavaScript programming statements and
expressions, and have a filename extension of ‘.js’
 To execute a Node.js program, run the Node.js executable for the given
environment and pass the name of the file (with or without the JS
extension) as the first argument
Running Node.js Program
Managing Packages with NPM
 NPM is an acronym for Node.js Package Manager
 NPM provides a public package repository, a specification for building
packages, and a command line tool for working with packages
 http://www.npmjs.com
 The company npm, Inc. develops and maintains NPM
 Node.js distributes the npm executable along with the node
executable, but it's actually a separate program with its own versioning
NPM Public Repository
https://www.npmjs.com/
Local vs Global Packages
 Packages can be installed locally or globally
 Local packages are stored locally in a project, in the node_modules
folder
 Global packages are stored globally on system
 Typically, local packages are code libraries used by project
 Typically, global packages are executables used to perform some
operation on a project such as running tasks
 Local packages are available only within their specific project, and
global packages are available system wide
Installing & Uninstalling Packages
 The npm program is used to
manage packages
 The first argument to the npm
program is the command to be
executed
 Packages can be installed with the
install command, and uninstalled
with the uninstall command
 There are many more commands
available for NPM
• The –global flag installs and
uninstalls the package globally,
without the global flag, the
packages are installed and
uninstalled locally
Global Packages on Windows
 Are global to the user,
not the system
 Does not require
administrative privileges
Globally Installed Packages are stored in:
C:Users<username>AppDataRoamingnpm
Global Packages on Mac and Linux
 By default, are global to
the system, not just the
user
 Requires administrative
privileges, unless
permissions are fixed
On a Mac by default, Globally Installed Packages are
stored in:
/usr/local/lib/node_modules
Fixing Permissions for Global Packages
https://docs.npmjs.com/getting-started/fixing-npm-permissions
Managing Packages with NPM
Building a Simple Web Server
 One of the core modules for Node.js is the HTTP module, which
provides a web server and web client
 The web server is very flexible, but requires a lot of boiler plate coding
to build even the simplest applications
 Commonly, other packages such as Express or Hapi are used to
configure the web server
 Web server are I/O intensive applications making them well suited for
Node.js
 Node.js is great for web servers because of its easy handling of JSON
data
Building a Simple Web Server
Reading Files from Disk
 Node.js allows full access to the system (such as accessing the file
system), unlike a web browser which only allows sandboxed access
 Accessing file system resources can be synchronously and
asynchronously
 Synchronous access can be used for initial program loading, but only
asynchronous access should be used during program operation
 Both text and binary data can read and written
 Full support for streams
Reading Files from Disk
Debugging Node.js
 Node.js comes with a built in command line debugger, but its limited
 Instead, there are many code editors and other tools which greatly
simplify debugging of Node.js applications
 StrongLoop's Node Inspector (free – Windows/Mac/Linux)
 Microsoft Visual Studio Code (free – Windows/Mac/Linux)
 Microsoft Visual Studio with Node.js Extension (community edition free – Windows only)
 GitHub's Atom with Node Debugger Package (free – Windows/Mac/Linux)
 JetBrains' WebStorm (not free – Windows/Mac/Linux)
 These IDEs provide the standard fare of debugging tools such as
breakpoint, call stacks, watches, and local variables
StrongLoop's Node Inspector
https://github.com/node-inspector/node-inspector
Debugging Node.js using Node Inspector
Microsoft's Visual Studio Code
https://code.visualstudio.com
Debugging Node.js using Visual Studio Code
Microsoft's Visual Studio with Node.js Extension
https://www.visualstudio.com/products/visual-studio-community-vs
Debugging Node.js using Visual Studio with Node Extension
GitHub's Atom with the Node Debugger Package
https://atom.io
Debugging Node.js using Atom with Node Debugger Package
JetBrains' WebStorm
https://www.jetbrains.com/webstorm
Debugging Node.js using WebStorm
Creating a Package
 All projects (which are also packages) need to be configured to work
with NPM
 The command npm init is used to configure a project
 It will ask a series of questions, all of which have default answers, that
are used to create and initialize a package.json file
 The package.json file contains metadata about the project, as well as,
a list of application and development dependencies
 When NPM packages are installed, NPM will register them with the
package.json file
Saving Package Dependencies
 Simply installing packages do not save the dependency in the
package.json file
 In addition to installing, additional flags need to be specified:
 --save or -S will save the package as an application dependency
 --save-dev or -D will save the package a development dependency
 Application dependencies are used by the Node.js program when
executing (common example would be Express)
 Development dependencies are used to develop the Node.js program
(common example would be Grunt)
Saving Package Dependencies
• The terminal commands to left, will
produce a package.json file similar to
the one on the right.
• The file is a JSON file, and can be edited
by hand
• Name is the name of the package
• Version follows the SEMVER scheme
• The version of each dependency is
tracked as well
• Main is the main file imported when
requiring the module
Terminal Commands Package.json
Creating a Package
Use NPM to run a package
 In addition to managing packages with NPM,
packages can be configured to be executed
with NPM
 To configure the execution of Node packages,
the scripts option of the package.json is
configured
 To run a Node.js program, the start script is
configured
Use NPM to run a package
Conclusion
 Node.js is a great platform building applications, especially
applications which are I/O intensive
 Working with Node.js involves many tools for managing packages,
debugging code, and packaging projects for deployment
 The Node Package Manager (NPM) is used to install packages, and
divide applications into smaller reusable parts
 Node.js can be used for development tooling, web applications and
general purpose applications

More Related Content

Similar to Mastering Node.js Fundamentals

Introduction to node.js By Ahmed Assaf
Introduction to node.js  By Ahmed AssafIntroduction to node.js  By Ahmed Assaf
Introduction to node.js By Ahmed AssafAhmed Assaf
 
Halton Software Peer 2 Peer Meetup #10
Halton Software Peer 2 Peer Meetup #10Halton Software Peer 2 Peer Meetup #10
Halton Software Peer 2 Peer Meetup #10David Ashton
 
An Introduction of Node Package Manager (NPM)
An Introduction of Node Package Manager (NPM)An Introduction of Node Package Manager (NPM)
An Introduction of Node Package Manager (NPM)iFour Technolab Pvt. Ltd.
 
Steps to Install NPM and Node.js on Windows and MAC
Steps to Install NPM and Node.js on Windows and MACSteps to Install NPM and Node.js on Windows and MAC
Steps to Install NPM and Node.js on Windows and MACInexture Solutions
 
Improving build solutions dependency management with webpack
Improving build solutions  dependency management with webpackImproving build solutions  dependency management with webpack
Improving build solutions dependency management with webpackNodeXperts
 
Node JS Express : Steps to Create Restful Web App
Node JS Express : Steps to Create Restful Web AppNode JS Express : Steps to Create Restful Web App
Node JS Express : Steps to Create Restful Web AppEdureka!
 
Node Session - 2
Node Session - 2Node Session - 2
Node Session - 2Bhavin Shah
 
Orchestrated Android-Style System Upgrades for Embedded Linux
Orchestrated Android-Style System Upgrades for Embedded LinuxOrchestrated Android-Style System Upgrades for Embedded Linux
Orchestrated Android-Style System Upgrades for Embedded LinuxKynetics
 
Orchestrated Android-Style System Upgrades for Embedded Linux
Orchestrated Android-Style System Upgrades for Embedded LinuxOrchestrated Android-Style System Upgrades for Embedded Linux
Orchestrated Android-Style System Upgrades for Embedded LinuxNicolaLaGloria
 
Understanding NuGet implementation for Enterprises
Understanding NuGet implementation for EnterprisesUnderstanding NuGet implementation for Enterprises
Understanding NuGet implementation for EnterprisesJ S Jodha
 
I Just Want to Run My Code: Waypoint, Nomad, and Other Things
I Just Want to Run My Code: Waypoint, Nomad, and Other ThingsI Just Want to Run My Code: Waypoint, Nomad, and Other Things
I Just Want to Run My Code: Waypoint, Nomad, and Other ThingsMichael Lange
 
Angular Part 3 (Basic knowledge)
Angular Part 3 (Basic knowledge)Angular Part 3 (Basic knowledge)
Angular Part 3 (Basic knowledge)Rohit Singh
 

Similar to Mastering Node.js Fundamentals (20)

Introduction to node.js By Ahmed Assaf
Introduction to node.js  By Ahmed AssafIntroduction to node.js  By Ahmed Assaf
Introduction to node.js By Ahmed Assaf
 
Halton Software Peer 2 Peer Meetup #10
Halton Software Peer 2 Peer Meetup #10Halton Software Peer 2 Peer Meetup #10
Halton Software Peer 2 Peer Meetup #10
 
Node.js
Node.jsNode.js
Node.js
 
An Introduction of Node Package Manager (NPM)
An Introduction of Node Package Manager (NPM)An Introduction of Node Package Manager (NPM)
An Introduction of Node Package Manager (NPM)
 
Steps to Install NPM and Node.js on Windows and MAC
Steps to Install NPM and Node.js on Windows and MACSteps to Install NPM and Node.js on Windows and MAC
Steps to Install NPM and Node.js on Windows and MAC
 
Node J pdf.docx
Node J pdf.docxNode J pdf.docx
Node J pdf.docx
 
Node J pdf.docx
Node J pdf.docxNode J pdf.docx
Node J pdf.docx
 
Improving build solutions dependency management with webpack
Improving build solutions  dependency management with webpackImproving build solutions  dependency management with webpack
Improving build solutions dependency management with webpack
 
Node JS Express : Steps to Create Restful Web App
Node JS Express : Steps to Create Restful Web AppNode JS Express : Steps to Create Restful Web App
Node JS Express : Steps to Create Restful Web App
 
Node Session - 2
Node Session - 2Node Session - 2
Node Session - 2
 
Node js crash course session 1
Node js crash course   session 1Node js crash course   session 1
Node js crash course session 1
 
Orchestrated Android-Style System Upgrades for Embedded Linux
Orchestrated Android-Style System Upgrades for Embedded LinuxOrchestrated Android-Style System Upgrades for Embedded Linux
Orchestrated Android-Style System Upgrades for Embedded Linux
 
Orchestrated Android-Style System Upgrades for Embedded Linux
Orchestrated Android-Style System Upgrades for Embedded LinuxOrchestrated Android-Style System Upgrades for Embedded Linux
Orchestrated Android-Style System Upgrades for Embedded Linux
 
NPM.pdf
NPM.pdfNPM.pdf
NPM.pdf
 
Android ndk
Android ndkAndroid ndk
Android ndk
 
Understanding NuGet implementation for Enterprises
Understanding NuGet implementation for EnterprisesUnderstanding NuGet implementation for Enterprises
Understanding NuGet implementation for Enterprises
 
I Just Want to Run My Code: Waypoint, Nomad, and Other Things
I Just Want to Run My Code: Waypoint, Nomad, and Other ThingsI Just Want to Run My Code: Waypoint, Nomad, and Other Things
I Just Want to Run My Code: Waypoint, Nomad, and Other Things
 
Angular Part 3 (Basic knowledge)
Angular Part 3 (Basic knowledge)Angular Part 3 (Basic knowledge)
Angular Part 3 (Basic knowledge)
 
Android NDK
Android NDKAndroid NDK
Android NDK
 
Node js Introduction
Node js IntroductionNode js Introduction
Node js Introduction
 

Recently uploaded

Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 

Recently uploaded (20)

Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 

Mastering Node.js Fundamentals

  • 1. Header Microsoft Virtual Academy Introduction to Node.js Eric W. Greene Produced by Mastering Node.js, Part 1
  • 2. Course Overview  What is Node.js?  Installing Node.js  Node REPL  Node Programs  Managing Packages with NPM  Building a Simple Web Server  Reading Files from Disk  Debugging  Creating a Package  Use NPM to run a package
  • 3. What is Node.js?  Node.js is a cross-platform, JavaScript environment on the server  JavaScript is a glue language for C++ modules  It is powered by Google’s V8 JavaScript engine  Node.js uses an event driven, non-blocking I/O model  Node.js is like a web browser with a different set of C++ modules  It provides modules for interacting with local system resources such as processes, file system, networking, etc…
  • 4. Node.js compared to a Web Browser JavaScript V8 DOM XHR Audio File Reader Timers Web Sockets Video Many More… JavaScript V8 File System Net Cluster HTTP Timer Process Stream Many More… Web Browser Node.js
  • 5. Installing Node.js  Node.js can be installed from a platform specific installer, precompiled source code, and compiled from source code  To download the installer: http://www.nodejs.org  When installing using the platform specific installer, most users accept the defaults
  • 6. Node.js Versions  The installer and pre-compiled binaries can be downloaded for either the Long-Term Support version or Current Version  Long-Term Support version is best for applications in production  The Current Version is best for working with the newest features
  • 8. Node REPL Environment  REPL stands for Read, Evaluate, Print, Loop  Allows the user to enter a JavaScript command, evaluate it, print the results, and loop to enter another command  Supports multiline commands, customize command prompt and preconfigured context  Sessions can be saved, and reloaded again  Useful for learning JavaScript or executing tasks from a JavaScript command line
  • 9. Using the Node REPL Environment
  • 10. Node.js Programs  Node.js programs can be created using JavaScript files which are executed with the Node.js executable  JavaScript files contain JavaScript programming statements and expressions, and have a filename extension of ‘.js’  To execute a Node.js program, run the Node.js executable for the given environment and pass the name of the file (with or without the JS extension) as the first argument
  • 12. Managing Packages with NPM  NPM is an acronym for Node.js Package Manager  NPM provides a public package repository, a specification for building packages, and a command line tool for working with packages  http://www.npmjs.com  The company npm, Inc. develops and maintains NPM  Node.js distributes the npm executable along with the node executable, but it's actually a separate program with its own versioning
  • 14. Local vs Global Packages  Packages can be installed locally or globally  Local packages are stored locally in a project, in the node_modules folder  Global packages are stored globally on system  Typically, local packages are code libraries used by project  Typically, global packages are executables used to perform some operation on a project such as running tasks  Local packages are available only within their specific project, and global packages are available system wide
  • 15. Installing & Uninstalling Packages  The npm program is used to manage packages  The first argument to the npm program is the command to be executed  Packages can be installed with the install command, and uninstalled with the uninstall command  There are many more commands available for NPM • The –global flag installs and uninstalls the package globally, without the global flag, the packages are installed and uninstalled locally
  • 16. Global Packages on Windows  Are global to the user, not the system  Does not require administrative privileges Globally Installed Packages are stored in: C:Users<username>AppDataRoamingnpm
  • 17. Global Packages on Mac and Linux  By default, are global to the system, not just the user  Requires administrative privileges, unless permissions are fixed On a Mac by default, Globally Installed Packages are stored in: /usr/local/lib/node_modules
  • 18. Fixing Permissions for Global Packages https://docs.npmjs.com/getting-started/fixing-npm-permissions
  • 20. Building a Simple Web Server  One of the core modules for Node.js is the HTTP module, which provides a web server and web client  The web server is very flexible, but requires a lot of boiler plate coding to build even the simplest applications  Commonly, other packages such as Express or Hapi are used to configure the web server  Web server are I/O intensive applications making them well suited for Node.js  Node.js is great for web servers because of its easy handling of JSON data
  • 21. Building a Simple Web Server
  • 22. Reading Files from Disk  Node.js allows full access to the system (such as accessing the file system), unlike a web browser which only allows sandboxed access  Accessing file system resources can be synchronously and asynchronously  Synchronous access can be used for initial program loading, but only asynchronous access should be used during program operation  Both text and binary data can read and written  Full support for streams
  • 24. Debugging Node.js  Node.js comes with a built in command line debugger, but its limited  Instead, there are many code editors and other tools which greatly simplify debugging of Node.js applications  StrongLoop's Node Inspector (free – Windows/Mac/Linux)  Microsoft Visual Studio Code (free – Windows/Mac/Linux)  Microsoft Visual Studio with Node.js Extension (community edition free – Windows only)  GitHub's Atom with Node Debugger Package (free – Windows/Mac/Linux)  JetBrains' WebStorm (not free – Windows/Mac/Linux)  These IDEs provide the standard fare of debugging tools such as breakpoint, call stacks, watches, and local variables
  • 26. Debugging Node.js using Node Inspector
  • 27. Microsoft's Visual Studio Code https://code.visualstudio.com
  • 28. Debugging Node.js using Visual Studio Code
  • 29. Microsoft's Visual Studio with Node.js Extension https://www.visualstudio.com/products/visual-studio-community-vs
  • 30. Debugging Node.js using Visual Studio with Node Extension
  • 31. GitHub's Atom with the Node Debugger Package https://atom.io
  • 32. Debugging Node.js using Atom with Node Debugger Package
  • 35. Creating a Package  All projects (which are also packages) need to be configured to work with NPM  The command npm init is used to configure a project  It will ask a series of questions, all of which have default answers, that are used to create and initialize a package.json file  The package.json file contains metadata about the project, as well as, a list of application and development dependencies  When NPM packages are installed, NPM will register them with the package.json file
  • 36. Saving Package Dependencies  Simply installing packages do not save the dependency in the package.json file  In addition to installing, additional flags need to be specified:  --save or -S will save the package as an application dependency  --save-dev or -D will save the package a development dependency  Application dependencies are used by the Node.js program when executing (common example would be Express)  Development dependencies are used to develop the Node.js program (common example would be Grunt)
  • 37. Saving Package Dependencies • The terminal commands to left, will produce a package.json file similar to the one on the right. • The file is a JSON file, and can be edited by hand • Name is the name of the package • Version follows the SEMVER scheme • The version of each dependency is tracked as well • Main is the main file imported when requiring the module Terminal Commands Package.json
  • 39. Use NPM to run a package  In addition to managing packages with NPM, packages can be configured to be executed with NPM  To configure the execution of Node packages, the scripts option of the package.json is configured  To run a Node.js program, the start script is configured
  • 40. Use NPM to run a package
  • 41. Conclusion  Node.js is a great platform building applications, especially applications which are I/O intensive  Working with Node.js involves many tools for managing packages, debugging code, and packaging projects for deployment  The Node Package Manager (NPM) is used to install packages, and divide applications into smaller reusable parts  Node.js can be used for development tooling, web applications and general purpose applications