Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that...

38
Programming in Cloud with Node.js Georgiana Calancea Ioana Bogdan

Transcript of Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that...

Page 1: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

Programming in Cloud with Node.js

Georgiana Calancea Ioana Bogdan

Page 2: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

What is Node.js?

• open-source server side runtime environment built on Chrome's V8 JavaScript engine;

• event driven, non-blocking (asynchronous) I/O and cross-platform runtime environment for building highly scalable server-side application using JavaScript;

• used to build different types of applications such as command line application, web application, real-time chat application, REST API server;

Page 3: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

Traditional Web Server Model• each request is handled by a dedicated thread from the thread pool;

• if no thread is available in the thread pool at any point of time then the request waits till the next available thread;

• a dedicated thread executes a particular request and does not return to thread pool until it completes the execution and returns a response;

Page 4: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

Node.js Process Model

• runs in a single process and the application code runs in a single thread and thereby needs less resources than other platforms;

• all the user requests to your web application will be handled by a single thread and all the I/O work or long running job is performed asynchronously for a particular request;

• the single thread doesn't have to wait for the request to complete and is free to handle the next request;

Page 5: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

Node.js Process Model

• when asynchronous I/O work completes, it processes the request further and sends the response;

• an event loop is constantly watching for the events to be raised for an asynchronous job and executes the callback function when the job completes;

• Node.js uses in its implementation libev for the event loop which in turn uses internal C++ thread pool to provide asynchronous I/O;

Page 6: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

Asynchronous Web Server Model

Page 7: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

Setup the dev environmentNode.js development environment can be setup in Windows, Mac, Linux and Solaris. The following tools/SDK are required for developing a Node.js application on any platform.

1. Node.js ( download from https://nodejs.org/en/ or install with Homebrew on MacOS or Linuxbrew on Linux OS )

2. Node Package Manager (NPM) - is bundled with Node.js

3. IDE (Integrated Development Environment) or TextEditor ( you can download Sublime Text 3 from https://www.sublimetext.com/)

Page 8: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

Node.js Console

• Node.js comes with virtual environment called REPL (aka Read-Eval-Print-Loop);

• quick and easy way to test simple code;

• to launch the REPL (Node shell), open command prompt (in Windows) or terminal (in Mac or UNIX/Linux) and type node;

Page 9: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

Node.js BasicsPrimitive Types

• String

• Number

• Boolean

• Undefined

• Null

• RegExp

Object Literal

Functions

Page 10: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

Node.js Core Modules• simple or complex functionality organised in single or multiple JavaScript files which can be reused throughout the application;

• each module in Node.js has its own context, so it cannot interfere with other modules or pollute global scope;

Page 11: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

Usage of Core Modules• require() function returns an object because http module returns its functionality as an object

• you can use its properties and methods using dot notation e.g. http.createServer().

Import a module using require():

var module = require('module_name');

Page 12: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

Local Modules• modules created locally in your Node.js application;

• these modules include different functionalities of your application in separate files and folders;

• module.exports is a special object which is included in every JS file in the Node.js application by default

• use module.exports or exports to expose a function, object or variable as a module in Node.js.

Page 13: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

Node Packaging Manager• NPM is a command line tool that installs, updates or uninstalls Node.js packages in your application.

• It is also an online repository for open-source Node.js packages. The node community around the world creates useful modules and publishes them as packages in this repository;

• All the modules installed using NPM are installed under node_modules folder.

the command will create an ExpressJS folder under node_modules folder (in the root folder of your project)

and will install Express.js there

it additionally saves the dependency in package.json

the command will install the Express.js dependency globally (NPM installs global packages into /<User>/local/

lib/node_modules folder)

Page 14: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

it starts a command line client that allows the configuration presented

in the package.json

updates the version for package express

uninstalls the package express

How do you uninstall a global module?

How do you update npm?

Page 15: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

CallbacksSimply put: A callback is a function that is to be executed after another function (normally asynchronous) has finished executing — hence the name ‘call back’.

Page 16: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

Promises

"Imagine you are a kid. Your mom promises you that she'll get you a new phone next week."

You don't know if you will get that phone until next week. Your mom can either really buy you a brand new phone, or stand you up and withhold the phone if she is not happy.

That is a promise. A promise has 3 states. They are:

1. Pending: You don't know if you will get that phone

2. Fulfilled: Mom is happy, she buys you a brand new phone

3. Rejected: Your mom is not happy, she withholds the phone

Page 17: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

Promises Example

Promise UsagePromise Definition

Page 18: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

Node.js File SystemNode.js includes fs module to access physical file system.

The fs module is responsible for all the asynchronous or synchronous file I/O operations.

Page 19: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until
Page 20: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until
Page 21: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

Write a script in Node.js that reads a file, modifies the content, saves it in another

location and deletes the old file.

Page 22: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

Node.js Web Server• Node.js provides capabilities to create your own web server which will handle HTTP requests asynchronously;

• you can use Apache to run a Node.js web application but it is recommended to use Node.js web server;

Page 23: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

Handling HTTP Requests

• The http.createServer() method includes request and response parameters which are supplied by Node.js.

• The request object can be used to get information about the current HTTP request e.g., url, request header, and data.

• The response object can be used to send a response for a current HTTP request.

Page 24: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

Express.js• web application framework for Node.js. It provides various features that make web application development fast and easy which otherwise takes more time using only Node.js;

• based on the Node.js middleware module called connect which in turn uses http module. So, any middleware which is based on connect will also work with Express.js;

• easy to configure and customize;

• allows you to define routes of your application based on HTTP methods and URLs.

• includes various middleware modules which you can use to perform additional tasks on request and response;

• easy to integrate with different template engines like Pug;

• allows you to define an error handling middleware;

• easy to serve static files and resources of your application;

• allows you to create REST API server;

• easy to connect with databases such as MongoDB, Redis, MySQL

Page 25: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

Run the above example using node app.js command and point your browser to http://localhost:5000. What will it show?

Page 26: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

Run the above example using node app.js command and point your browser to http://localhost:5000. What does it show now?

Page 27: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

Handle POST Request in Express

Page 28: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

Run the above example using node app.js command and point your browser to http://localhost:5000. Test the application.

Page 29: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

... and now for the Cloud part

Page 30: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

the term Cloud Computing refers to the on-demand delivery of IT resources via the Internet with pay-as-you-go pricing

Cloud computing has become the new normal

Page 31: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

Amazon Web Services (AWS) is a secure cloud services platform, offering compute power, database storage, content delivery and other functionality to help businesses scale and grow.

Page 32: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

... one success story

400,000 people are hosted on any given night

10M guests in one year after migration to Cloud solutions

Started from 24 instances in 2010 and got to over 1000

Page 33: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until
Page 34: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

An overview of services

EC2: Server configuration and hosting

S3: Data storage and movement

CloudFront: Deliver a better user experience

Route 53: The AWS DNS service

Cloudwatch: Monitor your AWS environment

Page 35: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

Lambda: Functions for optimized compute

AWS Config: Infrastructure management

Kinesis: Optimize data flow

DynamoDB: Fast, easy database access

Page 36: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until
Page 37: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

... wait until you build your first serverless web application

Page 38: Programming in cloudadria/teach/courses/Cloud... · 2019. 3. 16. · Your mom promises you that she'll get you a new phone next week." You don't know if you will get that phone until

Thank you for your time!