SlideShare una empresa de Scribd logo
1 de 40
Descargar para leer sin conexión
Getting Started with Node.js
JUSTIN REOCK | CHIEF ARCHITECT | OPENLOGIC BY PERFORCE
Who is this nerd?
3 perforce.com
Justin Reock
Chief Architect
OpenLogic by Perforce
Presenter
Justin has over 20 years’ experience
working in various software roles and is
an outspoken free software evangelist,
delivering enterprise solutions and
community education on databases,
integration work, architecture, and
technical leadership.
He is currently the Chief Architect at
OpenLogic by Perforce.
4 perforce.com
GitHub Repo for
Demos
• https://github.com/jreock/
getting-started-with-
node.js
• ColorsModule will require
an “npm install” – but
don’t worry I’ll teach you
how to do it!
5 perforce.com
What are we going to solve?
7 perforce.com
8 perforce.com
Asychronous Processing
• No one likes the spinning wheel of doom hope!
• Node.js is inherently asynchronous, blocking actions only when it
absolutely needs to
• This is ideal for our new(ish) world of parallel compute,
distributed compute, microservices, etc…
• Built on industry standards and vetted against thousands of
production systems
What else?
10 perforce.com
A Long Time Ago…
Backend
Silo
Front End
Silo
Backend
Servers
Front End
Servers
App
11 perforce.com
Behold, The Future!
DevOps Team
App Farm
App
Node allows us to code our front end and our backend using a single language
12 perforce.com
Node.js Overview
• Created by Ryan Dahl in 2009 to address concurrency issues.
• Dahl famously “built it over the weekend…”
• Built on Chrome’s V8 JavaScript engine.
• Single-threaded asynchronous event driven JavaScript framework. In-
progress tasks can easily be checked for completion.
• Vertical scaling can be achieved by adding CPU cores and adding a worker to
each core. This means resources can be shared (via the cluster module and
child_process.fork(…)).
• Using JavaScript on the browser as well as the server minimizes the
mismatch between the environments. Example: Form validation code does
not have to be duplicated on both client and server.
• Node.js objects maintain a list of registered subscribers and notifies them
when their state changes (‘Observer’ design pattern).
13 perforce.com
Why reinvent the wheel?
• npm is the node (sic) package manager for Node.js.
• npm enables JavaScript developers to share code to
solve common problems promoting reuse. This
allows developers to take advantage of the expertise
of a large community.
• Very active and vibrant community: As of February
2017, there were nearly 400,000 total packages on
npm.
14 perforce.com
• NPM is the fastest growing
and most prolific module
repository out there
• Over a million modules and
growing
Source: www.modulecounts.com – August 2019
15 perforce.com
Benefits of Node.js
Pros
• Lightweight: Low memory footprint
• Cross-platform environment. Server and client-
side can be written in JavaScript.
• Fast: Asynchronous and event driven
• Popularity
• Concurrency implementation is abstract (handled
behind the scenes).
• Full Stack Web Development: Front-end
developers can code on the backend.
• Extensibility
Cons
• 1.7 GB memory limit (64-bit)
• Vertical scaling challenge (but workarounds exist)
• Module versions can get confusing
• Open source governance is not flawless (See
LeftPad)
• Learning curve for developers not used to dealing
with an inherently asynchronous language
16 perforce.com
Node.js Web-Enabled Hello World
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type':
'text/html'});
response.end('Hello World');
}).listen(8081);
http://www.haneycodes.net/to-node-js-or-not-to-node-js/
17 perforce.com
Node Architecture
18 perforce.com
Node’s Asynchronous Event Loop
19 perforce.com
Installing Node
• This will vary by environment, but Node can
be installed in several different ways:
• Linux Package Manager (like yum, apt-get,
pkg, emerge)
• Standalone installer
(https://nodejs.org/en/download/)
• Official Docker Image
• Standalone Shell Script
20 perforce.com
Verifying Node Install
• Once Node has been installed in your
environment, check to make sure NPM has been
installed as well
• You can validate the installation by simply
checking the version of each binary from the
command line:
21 perforce.com
Demo: A Quick Node App
• Node applications can be run using the “node”
command line binary
• We’ll write a simple console hello world app
from scratch
• We’ll then execute it on the command line
22 perforce.com
Picking an IDE
• I’ll cut to the chase, I really like
VSCode for Node.js development
• Terminal integration is essential
when building your Node apps
• And the built-in debugger works
flawlessly with Node.js
• However, you have other options
• In the end, go with what is familiar
and available (and is not Notepad)
23 perforce.com
Picking an IDE
• We’ve all got our preferences, but make sure whatever
you choose meets at least the following requirements:
üApplication Debugger
üSyntax Highlighting
üESLint
üTerminal Integration
üNPM Integration
üIntelliSense (Code Completion)
üCode Beautifier
üDocker Integration
üNode_modules Integration
üSource Control Integration
24 perforce.com
• Cloud 9: https://c9.io/
• Online Code Editor
• Debugging
• Docker Integration: https://github.com/kdelfour/cloud9-docker
• Live Preview
• JetBrains WebStorm: https://www.jetbrains.com/webstorm/
• Code Completion
• Debugger
• Komodo IDE: http://www.activestate.com/komodo-ide
• Nodeclipse: http://www.nodeclipse.org/
• Eclipse with Node.js Plugins
25 perforce.com
Setting up VSCode
• Countless VSCode extensions exist
• Start With at least:
• Node Debug
• Node.js Extension Pack
• Take time to explore what is out there
26 perforce.com
Understanding Launch.json in VSCode
• In VSCode, application launch configuration is
controlled through a JSON file called “launch.json”
• This file will be referenced before VSCode launches
the app when debugging
• It describes the environment the application will
run in including environment variables, bootstrap
information, etc
• It is essential for debugging, and convenient for
launching unit and functional tests
27 perforce.com
Demo: Debugging an App in VSCode
• We’ll modify our HelloWorld example to store
our message in a variable
• Then we’ll create a launch configuration for our
project
• Finally, we’ll launch our project and inspect our
message variable
28 perforce.com
Node App Basic Lifecycle
• If you are using SCM
• Don’t forget relevant
‘ignore’ files
Create and Clone
SCM Repo*
• Run "npm init" in
new project folder
Initialize Project
• Get to it!
• If using SCM, commit
as normal!
Code!
• In target production
environment, run
"npm install" to
download necessary
dependencies
Build
* Just create new folder if
not using SCM
29 perforce.com
Node App Basic Lifecycle
* Just create new folder if
not using SCM
• If you are using SCM
• Don’t forget relevant
‘ignore’ files
Create and Clone
SCM Repo*
• Run "npm init" in
new project folder
Initialize Project
• Get to it!
• If using SCM, commit
as normal!
Code!
• In target production
environment, run
"npm install" to
download necessary
dependencies
Build
30 perforce.com
Initializing a Node App
• It’s development best practice to use npm to initialize a new
Node.js app
• This process will prompt the user for several standard pieces of
metainformation about the application
• Things like the application name, version, license, and bootstrap
class will be collected
• NPM will generate a build configuration file for the app called
package.json
• Package.json will describe to NPM everything needed to build
your Node.js app
Demo: Initializing a Node app
• Let’s turn our HelloWorld demo into a proper
Node application
• We’ll run npm init in the root folder and
answer the prompts
• Then we’ll look at what NPM has done for us
32 perforce.com
Understanding package.json
• After running “npm
init”, this file will be
generated in the
project root folder
• Our project currently
has no
dependencies, but
dependency
information is held
here too
{
"name": "helloworld",
"version": "1.0.0",
"description": "Hello World!",
"main": "hello.js",
"scripts": {
"test": "echo "Error: no test
specified" && exit 1"
},
"author": "Justin Reock",
"license": "GPL-3.0-or-later"
}
33 perforce.com
Working with dependencies
• Our final topic will introduce dependencies, or modules
• Most modern open-source languages have a set of open-source
modules that can be added to a project
• You can also create your own modules, whether private or freely
available
• Recall that the NPM repository provides these modules
• NPM can also be used to add dependencies to a project, in both
development and production environments, using the “npm
install” command
• Once NPM has installed a module into a project, a developer can
begin using it with the “require” directive inside the Node.js app
34 perforce.com
Example: Let’s Add Some Color
• The “colors” module for Node adds easy support for generating
console colors through escape commands
• You want your console output to be readable, and even fun!
• The colors module makes that easy, and we can add it with npm
install
• Note that the –-save directive will ensure that the module is saved
to package.json
npm install –-save colors
var colors = require(‘colors’);
console.log(colors.red.underline(‘This is red underlined text.’);
or
console.log(‘This is red underlined text.’.underline.red)
35 perforce.com
Demo: Adding Project Modules from NPM
• So, let’s add some color to our HelloWorld app!
• We’ll use npm to install the “colors” module
and make sure it’s contained in package.json
• Then we’ll ”require” that module, and use it in
our code
• BONUS: Remove the node_modules and
rebuild with npm install
Conclusion and wrap-up
37 perforce.com
What did we learn?
• Node.js is a language that focuses on concurrency and unified
development
• Dependencies (modules) and builds are assisted through npm
• Node is asynchronous by default
• Many IDEs exist for Node.js, and we have focused on VSCode
• Node projects should be initialized using ”npm init”
• Modules can be installed with “npm install –save [module]”
• Projects are built from package.json in other environments with “npm
install”
• Modules are included as variables in a project using the “require”
directive
38 perforce.com
What Next?
• Node is a huge subject, but, I’d focus on the following from
here:
• Understand Asynchronous Coding
• Get to Know Events and Streams
• Learn about Functional Programming
• Master your IDE’s Debugger
• Start Exploring Other Modules!
• Still So Much to Learn!!
39 perforce.com
Feel free to reach out -- I get lonely!
LinkedIn – Only Justin Reock in the world
apparently!
Twitter – @jreock - But I do get a little
political on there….
Blog - https://www.openlogic.com/blog
Email – justin.reock@roguewave.com
Questions?

Más contenido relacionado

La actualidad más candente

Symfony Day 2009 - Symfony vs Integrating products
Symfony Day 2009 - Symfony vs Integrating productsSymfony Day 2009 - Symfony vs Integrating products
Symfony Day 2009 - Symfony vs Integrating products
Xavier Lacot
 
Os Koziarsky
Os KoziarskyOs Koziarsky
Os Koziarsky
oscon2007
 
Understanding Android Benchmarks
Understanding Android BenchmarksUnderstanding Android Benchmarks
Understanding Android Benchmarks
Koan-Sin Tan
 
Dd13.2013.milano.open ntf
Dd13.2013.milano.open ntfDd13.2013.milano.open ntf
Dd13.2013.milano.open ntf
Ulrich Krause
 
Workflow for Development, Release and Versioning with OSGi / bndtools- Real W...
Workflow for Development, Release and Versioning with OSGi / bndtools- Real W...Workflow for Development, Release and Versioning with OSGi / bndtools- Real W...
Workflow for Development, Release and Versioning with OSGi / bndtools- Real W...
mfrancis
 

La actualidad más candente (20)

.NET Core in the Real World
.NET Core in the Real World.NET Core in the Real World
.NET Core in the Real World
 
Eclipse OMR: a modern toolkit for building language runtimes
Eclipse OMR: a modern toolkit for building language runtimesEclipse OMR: a modern toolkit for building language runtimes
Eclipse OMR: a modern toolkit for building language runtimes
 
Introduction to Containers and Docker for PHP developers
Introduction to Containers and Docker for PHP developersIntroduction to Containers and Docker for PHP developers
Introduction to Containers and Docker for PHP developers
 
Symfony Day 2009 - Symfony vs Integrating products
Symfony Day 2009 - Symfony vs Integrating productsSymfony Day 2009 - Symfony vs Integrating products
Symfony Day 2009 - Symfony vs Integrating products
 
Sutol 2016 - Automation is developer's friend
Sutol 2016 - Automation is developer's friendSutol 2016 - Automation is developer's friend
Sutol 2016 - Automation is developer's friend
 
Os Koziarsky
Os KoziarskyOs Koziarsky
Os Koziarsky
 
Understanding Android Benchmarks
Understanding Android BenchmarksUnderstanding Android Benchmarks
Understanding Android Benchmarks
 
Maven: from Scratch to Production (.pdf)
Maven: from Scratch to Production (.pdf)Maven: from Scratch to Production (.pdf)
Maven: from Scratch to Production (.pdf)
 
.NET Core Blimey! Windows Platform User Group, Manchester
.NET Core Blimey! Windows Platform User Group, Manchester.NET Core Blimey! Windows Platform User Group, Manchester
.NET Core Blimey! Windows Platform User Group, Manchester
 
Dd13.2013.milano.open ntf
Dd13.2013.milano.open ntfDd13.2013.milano.open ntf
Dd13.2013.milano.open ntf
 
.NET Core: a new .NET Platform
.NET Core: a new .NET Platform.NET Core: a new .NET Platform
.NET Core: a new .NET Platform
 
Zend con 2016 bdd with behat for beginners
Zend con 2016   bdd with behat for beginnersZend con 2016   bdd with behat for beginners
Zend con 2016 bdd with behat for beginners
 
Highly Surmountable Challenges in Ruby+OMR JIT Compilation
Highly Surmountable Challenges in Ruby+OMR JIT CompilationHighly Surmountable Challenges in Ruby+OMR JIT Compilation
Highly Surmountable Challenges in Ruby+OMR JIT Compilation
 
C++ on the Web: Run your big 3D game in the browser
C++ on the Web: Run your big 3D game in the browserC++ on the Web: Run your big 3D game in the browser
C++ on the Web: Run your big 3D game in the browser
 
Production Ready WordPress - WC Utrecht 2017
Production Ready WordPress  - WC Utrecht 2017Production Ready WordPress  - WC Utrecht 2017
Production Ready WordPress - WC Utrecht 2017
 
Dr. Strangelove, or how I learned to love plugin development
Dr. Strangelove, or how I learned to love plugin developmentDr. Strangelove, or how I learned to love plugin development
Dr. Strangelove, or how I learned to love plugin development
 
GraalVM - MadridJUG 2019-10-22
GraalVM - MadridJUG 2019-10-22GraalVM - MadridJUG 2019-10-22
GraalVM - MadridJUG 2019-10-22
 
Workflow for Development, Release and Versioning with OSGi / bndtools- Real W...
Workflow for Development, Release and Versioning with OSGi / bndtools- Real W...Workflow for Development, Release and Versioning with OSGi / bndtools- Real W...
Workflow for Development, Release and Versioning with OSGi / bndtools- Real W...
 
Net core
Net coreNet core
Net core
 
Why Python In Entertainment Industry?
Why Python In Entertainment Industry?Why Python In Entertainment Industry?
Why Python In Entertainment Industry?
 

Similar a Getting Started with Node.js

Similar a Getting Started with Node.js (20)

Intro to Node.js (v1)
Intro to Node.js (v1)Intro to Node.js (v1)
Intro to Node.js (v1)
 
Node.js
Node.jsNode.js
Node.js
 
Building Open-source React Components
Building Open-source React ComponentsBuilding Open-source React Components
Building Open-source React Components
 
Building Open-Source React Components
Building Open-Source React ComponentsBuilding Open-Source React Components
Building Open-Source React Components
 
How to Install Node.js and NPM on Windows and Mac?
How to Install Node.js and NPM on Windows and Mac?How to Install Node.js and NPM on Windows and Mac?
How to Install Node.js and NPM on Windows and Mac?
 
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
 
3 Things Everyone Knows About Node JS That You Don't
3 Things Everyone Knows About Node JS That You Don't3 Things Everyone Knows About Node JS That You Don't
3 Things Everyone Knows About Node JS That You Don't
 
ASP.NET 5 - Microsoft's Web development platform reimagined
ASP.NET 5 - Microsoft's Web development platform reimaginedASP.NET 5 - Microsoft's Web development platform reimagined
ASP.NET 5 - Microsoft's Web development platform reimagined
 
Android NDK
Android NDKAndroid NDK
Android NDK
 
Android ndk
Android ndkAndroid ndk
Android ndk
 
Deploy and Update Jakarta EE & MicroProfile applications with Paketo.pptx
Deploy and Update Jakarta EE & MicroProfile applications with Paketo.pptxDeploy and Update Jakarta EE & MicroProfile applications with Paketo.pptx
Deploy and Update Jakarta EE & MicroProfile applications with Paketo.pptx
 
Node js meetup
Node js meetupNode js meetup
Node js meetup
 
Oh the compilers you'll build
Oh the compilers you'll buildOh the compilers you'll build
Oh the compilers you'll build
 
Unikernel User Summit 2015: Getting started in unikernels using the rump kernel
Unikernel User Summit 2015: Getting started in unikernels using the rump kernelUnikernel User Summit 2015: Getting started in unikernels using the rump kernel
Unikernel User Summit 2015: Getting started in unikernels using the rump kernel
 
Who Needs Visual Studio?
Who Needs Visual Studio?Who Needs Visual Studio?
Who Needs Visual Studio?
 
Overview of Node JS
Overview of Node JSOverview of Node JS
Overview of Node JS
 
Node Summit 2016: Building your DevOps for Node.js
Node Summit 2016: Building your DevOps for Node.jsNode Summit 2016: Building your DevOps for Node.js
Node Summit 2016: Building your DevOps for Node.js
 
Dynamic Languages in Production: Progress and Open Challenges
Dynamic Languages in Production: Progress and Open ChallengesDynamic Languages in Production: Progress and Open Challenges
Dynamic Languages in Production: Progress and Open Challenges
 
Modern Web-site Development Pipeline
Modern Web-site Development PipelineModern Web-site Development Pipeline
Modern Web-site Development Pipeline
 
NCDevCon 2017 - Cross Platform Mobile Apps
NCDevCon 2017 - Cross Platform Mobile AppsNCDevCon 2017 - Cross Platform Mobile Apps
NCDevCon 2017 - Cross Platform Mobile Apps
 

Más de Justin Reock

Más de Justin Reock (9)

Open Source AI and ML, Whats Possible Today?
Open Source AI and ML, Whats Possible Today?Open Source AI and ML, Whats Possible Today?
Open Source AI and ML, Whats Possible Today?
 
Community vs. Commercial Open Source
Community vs. Commercial Open SourceCommunity vs. Commercial Open Source
Community vs. Commercial Open Source
 
Monitoring Java Applications with Prometheus and Grafana
Monitoring Java Applications with Prometheus and GrafanaMonitoring Java Applications with Prometheus and Grafana
Monitoring Java Applications with Prometheus and Grafana
 
Intro to React
Intro to ReactIntro to React
Intro to React
 
Integrating Postgres with ActiveMQ and Camel
Integrating Postgres with ActiveMQ and CamelIntegrating Postgres with ActiveMQ and Camel
Integrating Postgres with ActiveMQ and Camel
 
Node.js Deeper Dive
Node.js Deeper DiveNode.js Deeper Dive
Node.js Deeper Dive
 
Linux 101
Linux 101Linux 101
Linux 101
 
ZendCon - Integration and Asynchronous Processing with ActiveMQ and Camel
ZendCon - Integration and Asynchronous Processing with ActiveMQ and CamelZendCon - Integration and Asynchronous Processing with ActiveMQ and Camel
ZendCon - Integration and Asynchronous Processing with ActiveMQ and Camel
 
ZendCon - Linux 101
ZendCon - Linux 101ZendCon - Linux 101
ZendCon - Linux 101
 

Último

%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 

Último (20)

WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 

Getting Started with Node.js

  • 1. Getting Started with Node.js JUSTIN REOCK | CHIEF ARCHITECT | OPENLOGIC BY PERFORCE
  • 2. Who is this nerd?
  • 3. 3 perforce.com Justin Reock Chief Architect OpenLogic by Perforce Presenter Justin has over 20 years’ experience working in various software roles and is an outspoken free software evangelist, delivering enterprise solutions and community education on databases, integration work, architecture, and technical leadership. He is currently the Chief Architect at OpenLogic by Perforce.
  • 4. 4 perforce.com GitHub Repo for Demos • https://github.com/jreock/ getting-started-with- node.js • ColorsModule will require an “npm install” – but don’t worry I’ll teach you how to do it!
  • 6. What are we going to solve?
  • 8. 8 perforce.com Asychronous Processing • No one likes the spinning wheel of doom hope! • Node.js is inherently asynchronous, blocking actions only when it absolutely needs to • This is ideal for our new(ish) world of parallel compute, distributed compute, microservices, etc… • Built on industry standards and vetted against thousands of production systems
  • 10. 10 perforce.com A Long Time Ago… Backend Silo Front End Silo Backend Servers Front End Servers App
  • 11. 11 perforce.com Behold, The Future! DevOps Team App Farm App Node allows us to code our front end and our backend using a single language
  • 12. 12 perforce.com Node.js Overview • Created by Ryan Dahl in 2009 to address concurrency issues. • Dahl famously “built it over the weekend…” • Built on Chrome’s V8 JavaScript engine. • Single-threaded asynchronous event driven JavaScript framework. In- progress tasks can easily be checked for completion. • Vertical scaling can be achieved by adding CPU cores and adding a worker to each core. This means resources can be shared (via the cluster module and child_process.fork(…)). • Using JavaScript on the browser as well as the server minimizes the mismatch between the environments. Example: Form validation code does not have to be duplicated on both client and server. • Node.js objects maintain a list of registered subscribers and notifies them when their state changes (‘Observer’ design pattern).
  • 13. 13 perforce.com Why reinvent the wheel? • npm is the node (sic) package manager for Node.js. • npm enables JavaScript developers to share code to solve common problems promoting reuse. This allows developers to take advantage of the expertise of a large community. • Very active and vibrant community: As of February 2017, there were nearly 400,000 total packages on npm.
  • 14. 14 perforce.com • NPM is the fastest growing and most prolific module repository out there • Over a million modules and growing Source: www.modulecounts.com – August 2019
  • 15. 15 perforce.com Benefits of Node.js Pros • Lightweight: Low memory footprint • Cross-platform environment. Server and client- side can be written in JavaScript. • Fast: Asynchronous and event driven • Popularity • Concurrency implementation is abstract (handled behind the scenes). • Full Stack Web Development: Front-end developers can code on the backend. • Extensibility Cons • 1.7 GB memory limit (64-bit) • Vertical scaling challenge (but workarounds exist) • Module versions can get confusing • Open source governance is not flawless (See LeftPad) • Learning curve for developers not used to dealing with an inherently asynchronous language
  • 16. 16 perforce.com Node.js Web-Enabled Hello World var http = require('http'); http.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text/html'}); response.end('Hello World'); }).listen(8081); http://www.haneycodes.net/to-node-js-or-not-to-node-js/
  • 19. 19 perforce.com Installing Node • This will vary by environment, but Node can be installed in several different ways: • Linux Package Manager (like yum, apt-get, pkg, emerge) • Standalone installer (https://nodejs.org/en/download/) • Official Docker Image • Standalone Shell Script
  • 20. 20 perforce.com Verifying Node Install • Once Node has been installed in your environment, check to make sure NPM has been installed as well • You can validate the installation by simply checking the version of each binary from the command line:
  • 21. 21 perforce.com Demo: A Quick Node App • Node applications can be run using the “node” command line binary • We’ll write a simple console hello world app from scratch • We’ll then execute it on the command line
  • 22. 22 perforce.com Picking an IDE • I’ll cut to the chase, I really like VSCode for Node.js development • Terminal integration is essential when building your Node apps • And the built-in debugger works flawlessly with Node.js • However, you have other options • In the end, go with what is familiar and available (and is not Notepad)
  • 23. 23 perforce.com Picking an IDE • We’ve all got our preferences, but make sure whatever you choose meets at least the following requirements: üApplication Debugger üSyntax Highlighting üESLint üTerminal Integration üNPM Integration üIntelliSense (Code Completion) üCode Beautifier üDocker Integration üNode_modules Integration üSource Control Integration
  • 24. 24 perforce.com • Cloud 9: https://c9.io/ • Online Code Editor • Debugging • Docker Integration: https://github.com/kdelfour/cloud9-docker • Live Preview • JetBrains WebStorm: https://www.jetbrains.com/webstorm/ • Code Completion • Debugger • Komodo IDE: http://www.activestate.com/komodo-ide • Nodeclipse: http://www.nodeclipse.org/ • Eclipse with Node.js Plugins
  • 25. 25 perforce.com Setting up VSCode • Countless VSCode extensions exist • Start With at least: • Node Debug • Node.js Extension Pack • Take time to explore what is out there
  • 26. 26 perforce.com Understanding Launch.json in VSCode • In VSCode, application launch configuration is controlled through a JSON file called “launch.json” • This file will be referenced before VSCode launches the app when debugging • It describes the environment the application will run in including environment variables, bootstrap information, etc • It is essential for debugging, and convenient for launching unit and functional tests
  • 27. 27 perforce.com Demo: Debugging an App in VSCode • We’ll modify our HelloWorld example to store our message in a variable • Then we’ll create a launch configuration for our project • Finally, we’ll launch our project and inspect our message variable
  • 28. 28 perforce.com Node App Basic Lifecycle • If you are using SCM • Don’t forget relevant ‘ignore’ files Create and Clone SCM Repo* • Run "npm init" in new project folder Initialize Project • Get to it! • If using SCM, commit as normal! Code! • In target production environment, run "npm install" to download necessary dependencies Build * Just create new folder if not using SCM
  • 29. 29 perforce.com Node App Basic Lifecycle * Just create new folder if not using SCM • If you are using SCM • Don’t forget relevant ‘ignore’ files Create and Clone SCM Repo* • Run "npm init" in new project folder Initialize Project • Get to it! • If using SCM, commit as normal! Code! • In target production environment, run "npm install" to download necessary dependencies Build
  • 30. 30 perforce.com Initializing a Node App • It’s development best practice to use npm to initialize a new Node.js app • This process will prompt the user for several standard pieces of metainformation about the application • Things like the application name, version, license, and bootstrap class will be collected • NPM will generate a build configuration file for the app called package.json • Package.json will describe to NPM everything needed to build your Node.js app
  • 31. Demo: Initializing a Node app • Let’s turn our HelloWorld demo into a proper Node application • We’ll run npm init in the root folder and answer the prompts • Then we’ll look at what NPM has done for us
  • 32. 32 perforce.com Understanding package.json • After running “npm init”, this file will be generated in the project root folder • Our project currently has no dependencies, but dependency information is held here too { "name": "helloworld", "version": "1.0.0", "description": "Hello World!", "main": "hello.js", "scripts": { "test": "echo "Error: no test specified" && exit 1" }, "author": "Justin Reock", "license": "GPL-3.0-or-later" }
  • 33. 33 perforce.com Working with dependencies • Our final topic will introduce dependencies, or modules • Most modern open-source languages have a set of open-source modules that can be added to a project • You can also create your own modules, whether private or freely available • Recall that the NPM repository provides these modules • NPM can also be used to add dependencies to a project, in both development and production environments, using the “npm install” command • Once NPM has installed a module into a project, a developer can begin using it with the “require” directive inside the Node.js app
  • 34. 34 perforce.com Example: Let’s Add Some Color • The “colors” module for Node adds easy support for generating console colors through escape commands • You want your console output to be readable, and even fun! • The colors module makes that easy, and we can add it with npm install • Note that the –-save directive will ensure that the module is saved to package.json npm install –-save colors var colors = require(‘colors’); console.log(colors.red.underline(‘This is red underlined text.’); or console.log(‘This is red underlined text.’.underline.red)
  • 35. 35 perforce.com Demo: Adding Project Modules from NPM • So, let’s add some color to our HelloWorld app! • We’ll use npm to install the “colors” module and make sure it’s contained in package.json • Then we’ll ”require” that module, and use it in our code • BONUS: Remove the node_modules and rebuild with npm install
  • 37. 37 perforce.com What did we learn? • Node.js is a language that focuses on concurrency and unified development • Dependencies (modules) and builds are assisted through npm • Node is asynchronous by default • Many IDEs exist for Node.js, and we have focused on VSCode • Node projects should be initialized using ”npm init” • Modules can be installed with “npm install –save [module]” • Projects are built from package.json in other environments with “npm install” • Modules are included as variables in a project using the “require” directive
  • 38. 38 perforce.com What Next? • Node is a huge subject, but, I’d focus on the following from here: • Understand Asynchronous Coding • Get to Know Events and Streams • Learn about Functional Programming • Master your IDE’s Debugger • Start Exploring Other Modules! • Still So Much to Learn!!
  • 39. 39 perforce.com Feel free to reach out -- I get lonely! LinkedIn – Only Justin Reock in the world apparently! Twitter – @jreock - But I do get a little political on there…. Blog - https://www.openlogic.com/blog Email – justin.reock@roguewave.com