0% found this document useful (0 votes)
39 views

Reviewer in IPT - 093554

Uploaded by

brixbradix
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Reviewer in IPT - 093554

Uploaded by

brixbradix
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

Back-end Development with

Node.Js
What is Node.Js?
Node.js is an open-source server environment. Node.js uses JavaScript
on the server. The task of a web server is to open a file on the server
and return the content to the client.
Node.js has a built-in module called HTTP, which allows Node.js to
transfer data over the Hyper Text Transfer Protocol (HTTP). The HTTP
module can create an HTTP server that listens to server ports and gives
a response back to the client.
Building a back-end application with
Node.js
Design Your Data
Model Develop APIs
Define your data schema using a Create the necessary APIs using a
database of your choice, like framework like Express.js to handle
MongoDB or MySQL. requests and responses.

Implement
Integrate Third- Authentication &
Party Services Authorization
Integrate services like payment Secure your application by
gateways or cloud storage by implementing user authentication
utilizing relevant npm packages. and authorization mechanisms.
Best practices and tips for Node.js
development
Use Asynchronous
Code
01 Maximize performance by utilizing asynchronous patterns and avoiding
blocking operations.
Optimize Network
Traffic
02 Minimize network overhead by compressing responses, using caching
mechanisms, and reducing unnecessary data transfers.
Implement Error
Handling
03 Handle errors gracefully to prevent crashes and improve reliability.
Back-end Development with
Node.Js
Node.js is a server-side platform based on the JavaScript Engine in Google Chrome. It was
created by Ryan Dahl in 2009, and the most recent version is v0.10.36. This is a cross-platform
runtime environment for developing server-side and networking applications that are open
source. Node.js programs are written in JavaScript and run on the Node.js runtime on OS X,
Microsoft Windows, and Linux. Node.js also comes with a big library of JavaScript modules,
which makes developing Node.js web applications much easier.
Benefits of using Node.js for
back-end development
Lightning-Fast Scalability &
Performance Efficiency
Node.js's event-driven architecture and non-
Node.js's single-threaded event loop allows
blocking I/O provide superior performance,
for handling massive concurrency, providing
making it perfect for real-time applications.
better scalability and resource utilization.

JavaScript
Ecosystem
Node.js leverages the vast JavaScript ecosystem, including
npm (Node Package Manager), which offers an extensive
range of ready-to-use libraries and modules.
Reason Why Node.Js is Suitable for
Back-end Development:

01 Reusability 03 Huge Community


With the support of frameworks like Express.js The speed of a successful development cycle
and Meteor.js, JavaScript is a common is influenced by the presence of a vibrant
language for writing both backend and online community.
frontend code.
Productivity and Developer Node.js
02 Efficiency 04 Frameworks
A significant amount of developer time can be Some jobs are still difficult to accomplish with
saved by reducing context switching between Node.js, so various frameworks have been
several languages. created to help.
Most Popular Node.js
Frameworks
EXPRESS.JS METEOR.JS

Web application framework that provides An ideal framework for creating quick prototypes and
broad features for building web and mobile building larger application out of the those
application. prototypes.

SOCKET.IO KOA.JS

A library that enables low latency, A new web framework designed by the team behind
bidirectional and event-based Express, which aims to be a smaller, more expensive,
communication between a client and a and more robust foundation for web applications and
server. APIs.
THANK YOU!
DEFINITION

Express.js is a minimal and flexible web


application framework for Node.js, designed to
make it easier to build web applications and
APIs
BACKGROUND

Express.js, commonly referred to as Express, was created


by TJ Holowaychuk and initially released in 2010.

TJ Holowaychuk, a prolific developer in the Node.js


community, aimed to address the need for a minimal and
flexible web application framework for Node.js
WHY USE EXPRESS?

• Time Efficient
• Fast
• Money Efficient
• Easy to learn
• Asynchronous
F E AT U R E S

• Middleware
• Routing
• Templating
• Debugging
MIDDLEWARE

Is a request handler with access to the application’s


request-response cycle
ROUTING

Is a process of defining how an application responds to a


client request to a particular endpoint.
T E M P L AT I N G

It refers to the mechanism of dynamically generating HTML


content on the server side and sending it to the client as a
response.

• Pug
• EJS
• Mustache
• Handlebars
DEBUGGING

It is a process of identifying and fixing errors or issues in a


web applications built with the express.js.

• Debugger statement
• VS code debugger
A D VA N TA G E S

• Simplicity and Minimalism


• Fast and Lightweight
• Middleware Support
• Robust Error Handling
• Active Community
D I S A D VA N TA G E S

• Callbacks issue
• Code organization
• Security Concerns
• Minimalistic Features
H O W T O I N S TA L L E X P R E S S . J S

• Install Node.js
• Verify Node.js and npm Installation
• Create a New Express.js Project
• Navigate to the Project Directory
• Install Dependencies
• Start the Express.js Application
Introduction to Node.js
About Node.js
• Created by Ryan Dahl in 2009

• MIT License

• Based on Google Chrome JavaScript V8 Engine


What is Node.js?
Node.js is a platform built on chrome’s Javascript
runtime for easily building, fast and scalable
network application.
Node.js uses an event-driven, non-blocking
l/O model that make it lightweight and efficient,
perfect for data-intensive real-time application that
run accros distributed devices.
Other projects like Node.js
● Vert.x => Polygot programming

● Akka => Scala and Java

● Tornado => Python

● Libevent => C

● EventMachine => Ruby


Why node.js ?
● Non Blocking I/O
● V8 Javascript Engine
● Single Thread with Event Loop
● Windows, Linux, Mac
● 1 Language for Frontend and Backend
The idea behind node.js
• Perform asynchronous processing on single thread instead of classical multithread
processing, minimize overhead & latency, maximize scalability

• Scale horizontally instead of vertically

• Ideal for applications that serve a lot of requests but dont use/need lots of
computational power per request

• Not so ideal for heavy calculations, e.g. massive parallel computing

• Also: Less problems with concurrency


Node.js Event Loop
There are a couple of implications of this apparently very simple and
basic model

•Avoid synchronous code at all costs because it blocks the


event loop

•Which means: callbacks,


callbacks, and more callbacks
Blocking vs Non-Blocking
Example :: Read data from file and show data
Blocking
● Read data from file
● Show data
● Do other tasks
var data = fs.readFileSync( “test.txt” );
console.log( data );
console.log( “Do other tasks” );
Non-Blocking
Callback
● Read data from file

When read data


completed, show data
● Do other tasks

fs.readFile( “test.txt”, function( err, data ) {


console.log(data);
Node.js for….

● Web application
● Websocket server
● Ad server
● Proxy server
● Streaming server
● Fast file upload client
● Any Real-time data apps
● Anything with high I/O
Application of node.js
• NodeJS should be preferred to build Real-
Time Chats, Complex Single-Page
applications, Real-time collaboration tools,
Streaming apps, JSON APIs based
application.
• There are some frameworks of the node
which you can use to build your
applications. Some popular frameworks of
node are…Express.js, Socket.io, Koa.js,
Meteor.js, Sail.js.
The Built-in HTTP module
Node.js has a built-in module called
HTTP, which allows Node.js to transfer
data over the Hyper Text Transfer
Protocol (HTTP).
To include the HTTP module, use the
require() method:

var http = require('http');


Node.js as a Web Server
The HTTP module can create an HTTP server
that listens to server ports and gives a respo
nse back to the client.

Use the createServer() method to create a


n HTTP server:

The function
passed into
the
http.createSer
Advantages of node.js
•Easy Scalability
•Real-time web apps
•Fast suite
•Easy to learn code
•Advantageod caching
•Data Streaming
•Hosting
•Corporate support
Concept of node.js
The following diagram depicts some important parts of Node.js that are useful and help us
understand it better.
• GLOBAL- global objects in node.js is available in all modules.These objects are
functions, module, strings etc.

Some node.js global objects


• ERROR HANDLING- errors in node.js are handled through
exceptions.

• STREAMS - streams are objects that let you write data or read
data continuously.

There are four types of streams.


-Readable
-Writable
-Duplex
-Transform
• BUFFER - is a module that allowshandling streams that contain
only binary data.

• DOMAIN - module intercepts errors that remain unhandled.

Two methods for intercepting.


-internal binding
-external binding
• DNS - DNS module is used to connect to dns server and
perform name resolution.

DNS module can also be used for performing name


resolution without a network communication by using the
method dns.resolve()

•DEBUGGER - Debugger can be used in the terminal by using


the "inspect" keyword before the name of JavaScript file.
JavaScript
ES6 New
Features
JavaScript
Is a text-based programming language used both on the client-side
and server-side that allows you to make web pages interactive.
Where HTML and CSS are languages that give structure and style
to web pages, JavaScript gives web pages interactive elements
that engage a user.
WHAT IS ES6?
ES6 stands for ECMAScript 6. ECMAScript was created to standardize
JavaScript, and ES6 is the 6th version of ECMAScript, it was published
in 2015, and is also known as ECMAScript 2015.
• THE OPERATOR
• ARRAY FROM()
• ARROW FUNCTIONS
ES6 NEW •

MULTI-LINE STRINGS
DEFAULT PARAMETERS

FEATURES •

TEMPLATE LITERALS
DESTRUCTURING
ASSIGNMENT
• ENHANCED OBJECT
LITERALS
• PROMISES
• CLASSES
• MODULES
1. The Operator

In mathematics and
computer programming, an
operator is a character that
represents a specific
mathematical or logical 2 + 3;//5
action or process.
2. Array Form()

Is used to create a new Console.log(Array.from(“This is


array instance from a given JavaScript Array” + “from() Method”));
array. In the case of a
string, every alphabet of
the string is converted to Output:
an element of the new T,h,I,s, ,i,s, ,J,a,v,a,S,c,r,I,p,t, ,A,r,r,a,y,
array instance and in the ,f,r,o,m,(,), ,M,e,t,h,o,d
case of integer values, a
new array instance simply
takes the elements of the
given array.
3. Arrow Function

It provides a more concise


syntax for writing function
expressions by removing const add = function(x,
the "function" and "return" y) {
keywords. return x + y;
Arrow functions are };
defined using the fat arrow
(=>) notation. const add = ( x, y ) => x
+y;
4. Multi-line Strings

ES6 also provides Multi- const multiLineString =


This is a multi – line string.
line Strings. Users can you can include line breaks
create multi-line strings by easily.
using back-ticks(`). you can also embed variable
like ${name}.
;

const name = “john”;


5. Default
Parameters ES6

function greet(name = “Guest”)


{console.log(‘ Hello, ${name} ! ‘ );

}
In ES6, users can provide
the default values right in Greet(); // Output: Hello, Guest!
Greet(“Alice”); // Output: Hello, Alice!
the signature of the
functions. But, in ES5, OR
operator had to be used. ES5

function greet(name) { name = name II


“Guest”; console.log(“Hello, “ + name
+ “!”);

greet (); // Output: Hello, Guest!


greet(“Alice”), // Output: Hello, Alice!
6. Template Literals
const name = “Alice”;
const age = 30;
ES6 introduces very simple
string templates along with // Using template literals to
placeholders for the create a string with
variables. The syntax for placeholders
using the string template is const greeting = ‘Hello,
${PARAMETER} and is used $(name)! You are $(age)
inside of the back-ticked years old.’;
string.
console.log(greeting);
7. Destructuring
Assignment
Destructuring is one of the
most popular features of DESTRUCTURING ASSIGNMENT:
ES6. The destructuring
assignment is an Const numbers = [1, 2, 3, 4, 5];
expression that makes it Const first = [first, second, …rest] =
easy to extract values from
arrays, or properties from numbers;
objects, into distinct
variables.
7. Destructuring
Assignment

REGULAR CODE: DESTRUCTURING ASSIGNMENT:

const.numbers = [1, 2, 3, 4, 5]; Const numbers = [1, 2, 3, 4, 5];

const first = numbers[0]; Const first = [first, second, …rest] =

const second = numbers[1] numbers;

const rest = numbers.slice(2);


8. Enhanced Object
Literals
ES6 provides enhanced const name = “John”;
object literals which make const age = 30;
it easy to quickly create
objects with properties const person = {
name,
inside the curly braces. age,
greet(){
console.log(‘Hello, my name is
$ {this.name} and I am
${this.age} years old.’);
}
};
9. Promises
function fetchData() {
return new Promise((resolve, reject) => {
// Simulating an asynchronous operation
Promises are used for setTimeout(() => {
asynchronous execution. const data = ‘This is the fetched data’;
We can use promise with resolved(data);
the arrow function. }, 2000);
});
}

fetchData()
.then((data) => {
Console.log(data);// Output: This is the
fetched data
})
.catch((error) => {
console.error(error);
});
10. Classes
Previously, classes never Class Person {
existed in JavaScript. Constructor(name, age) {
Classes are introduced in This.name = name;
ES6 which looks similar to This.name = name;
classes in other object- This.age = age;
}
oriented languages, such
as C++, Java, PHP, etc. But, Greet(){
they do not work exactly Console.log(‘Hello, my name
the same way. ES6 classes is $ {this.name} and I am
make it simpler to create ${this.age} years old.’);
objects, implement }
inheritance by using the }
"extends" keyword and
also reuse the code
efficiently.
11. Modules

Previously, there was no // math.js


native support for modules Export const add = (a, b) => a + b;
in JavaScript. ES6 Export const subtract = (a, b) => a – b;
introduced a new feature
called modules, in which //main.js
each module is Import {add, subtract} from ‘./math.js’;
represented by a separate
".js" file. We can use the Console.log(add(2, 3)); //5
"import" or "export"
statement in a module to
import or export variables,
functions, classes or any
other component from/to
different files and modules.
SUMMARY

• In this article, we learned about the


ECMAScript 2015 or ES6, which defines the
standard for JavaScript implementation .
• We also learned about the top 10 features of
ES6 which makes it very popular.
• ES6 provides classes, modules , arrow
functions, template literals, destructuring
assignments and many more features which
make working with JavaScript a lot easier.

You might also like