INTRODUCTION TO By Stepan Vardanyan CONTENT Theory 1
INTRODUCTION TO By Stepan Vardanyan
CONTENT Theory: 1. Blocking vs Non blocking i/o. 2. V 8, Libuv, node bindings. 3. node. js history & philosophy. 4. Npm & Frameworks. 5. Comparison. Practice: 1. Project structure, modules. 2. C++ addons for node. js + example.
Resources CPU RAM HDD Network
Problem - Blocking I/O
Solution 1 - Multithreading
Solution 2 - Non-blocking I/O
Reactor design pattern Application 1 Request i/o 5 b Execute Handler 5 a 4 Event Queue Event Demultiplexer Resource Operation 6 Resource Operation Event Handler Handler Event Loop 2 3
VS
VS
Node. js Timeline 1. Initial release in 2009. 2. Express. js 1. 0 has been released in 2010. 3. Socket. io initial release in 2010 4. npm has been released in 2011. 5. iojs separated from node. js in 2014. 6. io. js and node. js had been merged into Ryan Dahl - node. js creator node. js version 4. 0 ( first release version of node. js ) in 2015. 7. Current version is 7. 7. x (april 2017)
Google V 8 is google’s open source, high performance, cross browser javascript engine written in C++. Developed in 2008 for Google Chrome, now used in node. js, opera, vivaldi and electron. V 8 compiles and executes javascript source code, handles memory allocation for objects and garbage collects objects it no longer need.
Libuv - Multiplatform C library Provides support for asynchronous i/o based on event loops. Full featured event loop backed by unix-epoll & kqueue, windows. IOCP, solaris - event ports. Primarily designed for use in node. js, but it is also used in other software projects.
Node. js bindings Reusability Performance Bindings simply expose core Node. js internal libraries written in C/C++ (c-ares, zlib, Open. SSL, http-parser, etc. ) to Java. Script. System programming languages such as C/C++ are generally much faster than other high-level languages.
So what node. js exactly is? - “Node. js is a Java. Script runtime built on Chrome's V 8 Java. Script engine. Node. js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. ” official documentation Javascript engine - compiles and executes js code Cross platform async i/o layer
Node package manager - npm Developed in 2010 by Isaac Z. Schlueter npm has : 400. 000+ packages 200. 000+ users registered Npm is largest ecosystem of open source libraries in the world! npm install / update / uninstall {package. Name}@{version} “-g” - globally, “-s” - updating in package. json
Node. js Philosophy - “The Node way” Node. js extends UNIX philosophy’s postulats: 1. Small is beautiful. 2. Make each program do one thing well. Node. js postulats: 1. Small core - large userland. 2. Callback comes last - error first.
Callback and Error handling input. txt file: “Hello” Synchronous code Asynchronous code var fs = require("fs"); var data = fs. read. File. Sync('input. txt'); console. log(data. to. String()); console. log("World"); fs. read. File('input. txt', function (err, data) { if (err) { return console. error(err); } console. log(data. to. String()); }); console. log("World"); > Hello > World > Hello
Node. js frameworks
Electron - cross platform framework for desktop applications based on node. js. Electron is an open source framework for creating native applications with web technologies like Java. Script, HTML, and CSS. It takes care of the hard parts so you can focus on the core of your application.
Companies are using node. js
Node. js at Pay. Pal In 2013 Pay. Pal rewrited some of services from java to node. js.
Node. js at Pay. Pal Development Built almost twice as fast with fewer people. Written in 33% fewer lines of code. Constructed with 40% fewer files. Performance Double the requests per second vs. the Java application. 35% decrease in the average response time. This resulted in the pages being served 200 ms faster — something users will definitely notice.
Reasons to move to Node. js - “Any application that can be written in Java. Script, will eventually be written in Java. Script”. Jeff Atwood, co-founder of Stack Overflow - "Node allows you to easily build applications with high confidence in build quality. Unit testing is easier. Integration testing is easier. REST is easier. Deployments are easier. " Stephen Comissio, senior. NET developer at Go. Daddy - "We’ve been doing a lot of things twice. Pretty much had to write everything twice - once for the server and once for the client. " Kim Trott, director of UI platform engineering at Netflix - “The thing that I like best about Node is the amount of power that I've personally found it gives me. ” Tom Croucher, Staff site reliability engineer at Uber Technologies
- Slides: 26