0% found this document useful (0 votes)
165 views25 pages

Introduction To Node - Js

The document provides an introduction to Node.js, including examples of using it to create HTTP and TCP servers and connect to MongoDB. It discusses Node.js' event loop model and non-blocking I/O. Good uses of Node.js include real-time services, web applications, command line tools, and desktop applications. Node.js is best for high concurrency needs where CPU cycles are not critical; it may not be the best choice for CPU-intensive tasks. Popular Node.js modules include Express, Jade, Socket.IO, and Nodemon.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
165 views25 pages

Introduction To Node - Js

The document provides an introduction to Node.js, including examples of using it to create HTTP and TCP servers and connect to MongoDB. It discusses Node.js' event loop model and non-blocking I/O. Good uses of Node.js include real-time services, web applications, command line tools, and desktop applications. Node.js is best for high concurrency needs where CPU cycles are not critical; it may not be the best choice for CPU-intensive tasks. Popular Node.js modules include Express, Jade, Socket.IO, and Nodemon.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 25

The server-side

JavaScript
Siddharth & Swarna
What to expect ahead?

➔ Introduction
➔ Examples
➔ Event Loops
➔ What can you do with Node.js?
➔ Node.js Ecosystem
➔ When to use Node.js?
➔ When to not use Node.js?
➔ Resources to get started with Node.js
Background

➔ V8 is an open-source JavaScript engine developed by Google for Chromium based web


browsers and is written in C++.

➔ Node.js runs on V8.

➔ It was created by Ryan Dahl in 2009.

➔ Its current version is 15.4.0 and LTS is 14.15.1.

➔ It is open-source and platform independent.


Introduction

➔ In simple words Node.js is ‘server-side JavaScript’.

➔ In not-so-simple words, Node.js is a high-performance network applications framework, well


optimized for high concurrent environments.

➔ It’s a command line tool.

➔ In Node.js, ‘js’ doesn’t mean that its solely written in JavaScript. It comprises of 40% JavaScript
and 60% C++.
Introduction - continued...

➔ Node.js uses an event-driven, non-blocking I/O model, which makes it lightweight.

➔ It makes use of event-loops via JavaScript’s callback functionality to implement the non-
blocking I/O.

➔ Programs for Node.js are written in JavaScript but not in the same JavaScript we are use to.
There is no DOM implementation provided by Node.js, i.e. you can not do this:

var element = document.getElementById(‘elementId’);

➔ Everything inside Node.js runs in a single-thread.


Getting started & hello world

➔ Install the latest LTS version of Node.js from https://nodejs.org/en/

➔ Open an editor of your choice and start typing JavaScript.

➔ Save the file with a ‘.js’ extension.

➔ Run it by using the following command

node filename.js
Example 1 - Hello
world
Following code outputs Hello
followed by world after 3s.
Event loops

➔ The event loop is what allows Node.js to perform non-blocking I/O operations

➔ Event-loops are the core of event-driven programming, almost all the UI programs use event-
loops to track the user event, for example: Clicks, Ajax
Client Clients send HTTP requests to
Node.js server.

JavaScript C++

Event loop returns


result to client. Node standard library
Event loop
(main thread)

Node bindings
(socket, http, etc)

Thread Event
V8 pool loop DNS Crypto
(libeio) (libev) (c-ares) (OpenSSL)

Response is sent back


to the event loop via C++ Threadpool
(worker threads)
Long running jobs run on
callback. worker threads.
Blocking I/O
Non-Blocking I/O
What can you do with Node.js?

➔ Package management through npm, bower, jspm, etc.

➔ Development tooling (module management with webpack, task running and automation through
grunt or gulp, linters like eslint or jslint, etc)

➔ Creation of back-end web applications.

➔ Command line tools like rimraf.

➔ Desktop applications.

➔ Building neural networks, or chat bots


Example 2 - HTTP
Server
Following code creates an HTTP
Server and prints ‘Hello World’
on the browser.
Example 3 - TCP
Server
Here is an example of a simple
TCP server which listens on
port 3000 and echoes whatever
you send it.
Node.js Ecosystem

➔ Node.js heavily relies on modules, in previous examples require keyword loaded the http & net
modules.

➔ Creating a module is easy, just put your JavaScript code in a separate js file and include it in
your code by using keyword require, like:
➔ Libraries in Node.js are called packages and they can be installed by typing

➔ NPM (Node Package Manager) comes bundled with Node.js installation.


Example 4 - Connect to MongoDB

Install MongoDB using npm, the official MongoDB driver for Node.js.
Example 5 - Twitter streaming
When to use Node.js?

➔ Node.js is good for creating streaming based real- time services, web chat applications, static file
servers etc.

➔ If you need high level concurrency and not worried about CPU-cycles.

➔ If you are great at writing JavaScript code because then you can use the same language at both
the places: server-side and client-side.
When not to use Node.js?

➔ When you are doing heavy and CPU intensive calculations on server side, because event-loops
are CPU hungry.

➔ Node’s relational database support tools are not up to the expected level when compared to other
languages.
Resources to get started

➔ Visit https://nodejs.org/en/ for more information about Node.js

➔ For more on MongoDB: https://www.mongodb.com/


Some good modules

➔ Express – to make things simpler e.g. syntax, DB connections.

➔ Jade – HTML template system

➔ Socket.IO – to create real-time apps

➔ Nodemon – to monitor Node.js and push change automatically

➔ Typescript – for strictly typed and runtime-error free JavaScript development


Thank you

You might also like