3. Introduction
• Open source, cross browser, runtime environment
• Executes javascript code on the server
• Build backend services
• Highy scalable, data intensive, and real time apps,
NOT for CPU intensive apps
3
4. Introduction
• Fewer lines, fewer files, fewer developers, more
requests, faster response
• JavaScript everywhere
• Clean, consistent, similar code on FE and BE
• Latest ECMAScript standards
• Many open source libraries
• Asynchronous I/O (unlike asp.net, ruby ...)
4
5. Introduction
• Who uses Node.js?
• GoDaddy, Groupon, IBM, LinkedIn, Microsoft,
Netflix, PayPal, SAP, Walmart, Yahoo, Amazon
Web Service
5
6. Introduction
• Browser = JS engine + browser code
• Nodejs = JS engine + C/C++ code
• Javasciprt = a programming language
• Nodejs = a runtime
• Express, Nest etc. = Nodejs Frameworks
6
9. Nodejs module system
• Module = sub program, packages as a unit
• CommonJS module system
• Each file is a separate module
• Use “require”
• Private & Public
• Module wrapper function
9
11. Npm, Yarn
• Download and manage dependencies
• Also used in frontend javascript projects
• Yarn: an alternative to npm
• Yet another resource negotiator
• Npm: after few years on Node.js
• Yarn (by facebook): to fix performance and security
concerns of npm
• yarn licenses list
• Yarn why package-name
11
15. Asynchronous Programming
• JavaScript is synchronous by default and is single
threaded.
• Computers are asynchronous .
• Multiple process could be executed at the same
time.
• Normally, programming languages are
synchronous, and some provide a way to manage
asynchronicity, in the language or through
libraries.
15
16. Asynchronous Programming
• Node.js introduced a non-blocking I/O
environment to extend this concept to file access,
network calls and so on
• There are different ways to achieve asynchronous
programming in node js.
16
17. Different ways of Asynchronous
Programming
Need of asynchronous -:
Different I/O operations(File read/write,DB
operations,making http calls)
• Callbacks
• Promises
• Async-await
17
18. Callbacks
A callback is a simple function that's passed as a
value to another function and will only be executed
when the event happens.
(Events could be button click, api call, customized
event)
Demo-
18
19. Callbacks
• Callbacks are great for simple cases!
• However every callback adds a level of nesting,
and when you have lots of callbacks, the code
starts to be complicated very quickly.
19
20. Promises
A promise is an object that may produce a single
value some time in the future: either a resolved
value, or a reason that it’s not resolved (e.g., a
network error occurred)
20
21. Promises
• Once a promise has been called, it will start in
pending state. This means that the caller function
continues the execution, while it waits for the
promise to do its own processing, and give the
caller function some feedback.
Demo-
21
22. Async-Await
• Promises were introduced to solve the famous
callback hell problem, but they introduced
complexity on their own, and syntax complexity
• They were good primitives around which a better
syntax could be exposed to the developers, so
when the time was right we got async functions.
• They make the code look like it's synchronous, but
it's asynchronous and non-blocking behind the
scenes.
22
23. Async-Await
Async-
It simply allows us to write promises based code as
if it was synchronous and it checks that we are not
breaking the execution thread. It operates
asynchronously via the event-loop. Async functions
will always return a value. It makes sure that a
promise is returned and if it is not returned then
javascript automatically wraps it in a promise which
is resolved with its value.
23
24. Some essential core modules
While numerous libraries are available for extending
Node's functionalities, the engine comes with a set
of core modules implementing basic functionalities.
There's currently 34 core modules included in Node.
fs,path,os,events,http
24
25. fs
The fs module provides a lot of very useful
functionality to access and interact with the file
system.
All methods in this module are async in nature but
we can get sync as well by adding sync.
25
26. event,http
• The events module provides us the EventEmitter
class, which is key to working with events in Node.
• The http module of Node.js provides useful
functions and classes to build an HTTP server
26
27. Writing testcases in NodeJS
• Testing is a key element to any application. For
Node.js, the framework available for Testing is
called Jasmine. In early 2000, there was a
framework for testing JavaScript applications
called JsUnit. Later this framework got upgraded
and is now known as Jasmine.
27