Node.js uses JavaScript - a language known to millions of developers worldwide - thus giving it a much lower learning curve even for complete beginners. Using Node.js you can build simple Command Line programs or complex enterprise level web applications with equal ease. Node.js is an event-driven, server-side, asynchronous development platform with lightning speed execution. It is open source and cross-platform, running on OS X, Linux and Windows. It also has an open source community that produces amazing modules available for use in your project for additional functionalities. Node.js helps you to code the most complex functionalities in just a few lines of code.
4. Slide 4 www.edureka.co/mastering-node-jsSlide 4
What is Node.js ?
Node.js is an open source, cross-platform runtime environment for server-side and networking applications
Node.js applications are written in JavaScript, and can be run within the Node.js runtime on OS X, Microsoft
Windows, Linux, FreeBSD, NonStop and IBM. -- Wikipedia
This is based on Google’s V8 JavaScript Engine
5. Slide 5 www.edureka.co/mastering-node-jsSlide 5
What is Node.js ? (Contd.)
Guess What ?
» IT’s SINGLE THREADED !!
» No worries about : race conditions, deadlocks and other problems that go with multi-threading.
» “Almost no function in Node directly performs I/O, so the process never blocks. Because nothing blocks,
less-than-expert programmers are able to develop scalable systems.” - (courtesy : nodejs.org)
Event
Loop
Event
Queue
Thread Pool
file system
network
process
other
6. Slide 6 www.edureka.co/mastering-node-jsSlide 6
Basics of Node.js : npm
npm used to stand for Node Package Manager. However it is not an acronym anymore. npm is not a Node.js
specific tool
npm is a registry of reusable modules and packages written by various developers
» Yes, you can publish your own npm packages
There are two ways to install npm packages :
» Locally : To use and depend on the package from your own module or project
» Globally: To use across the system, like a command line tool
7. Slide 7 www.edureka.co/mastering-node-jsSlide 7
Use-Cases of Node.js
1. Walmart executives believe that the benefit of using Node.js was
far greater than any risk in adopting a new technology
2. They re-engineered their mobile app to run on Node.js where all
the front end code gets executed on back-end
3. “We rely on services all over the world,” says Almaer (V.P Mobile
Architecture) “We do not control all of those services. Node
allows us to front all these services… and scale up very nicely.
It’s perfect for what we’re doing in mobile.”
Server Side Web Applications
1. Server Side Web Applications
Advantages
8. Slide 8 www.edureka.co/mastering-node-jsSlide 8
Use-Cases of Node.js (Contd.)
1. Linkedin is also leaning heavily on Node.js (Linkedin mobile and
tablet app is 95% html/web based)
2. “We’re still full-on Node. We are excited that it can scale,” says
Kiran Prasad (Head of Linkedin’s Mobile Development Team).
“Over the past few months, we’ve made performance tweaks so
we can scale even more. On four boxes, we can now handle 20
times the load we were handling before.”
Highly Scalable
1. Server Side Web Applications
2. Highly Scalable
Advantages
9. Slide 9 www.edureka.co/mastering-node-jsSlide 9
Use-Cases of Node.js (Contd.)
1. When he first built Voxer, Matt Ranney (Voxer’s CTO) ran a test
to see how many connections he could open on a single server.
"I just decided to open as many connections as I could, just to
see where things would fall down," Ranney says
2. "With Node, I could open, well, all of them. I couldn't open any
more connections without getting more IP addresses on my test
machine. Node uses such small amounts of memory, it's
astounding. I ran out of port numbers."
Low Memory Consumption
1. Server Side Web Applications
2. Highly Scalable
3. Low Memory Consumption
Advantages
10. Slide 10 www.edureka.co/mastering-node-jsSlide 10
Express JS
Express JS : Web Framework for Node.js. It is an npm package
To install : npm install -g express //in the main project folder
Express JS also provides a tool to create a simple folder structure for your app :
» Now, saying express newapp will create a project called newapp in the current working directory along
with a simple folder structure.
» The dependencies will then be installed by doing a cd newapp and then npm install
» To run the app
» node app // This is where the server is run and listening on a port
11. Slide 11 www.edureka.co/mastering-node-jsSlide 11
Express JS : Templates
By default Express supports the Jade Template Engine for the HTML Views.
What is an Javascript Template Engine : it is a framework to help bind data to your HTML views
Why do you need one ?
» Helps you easily bind data from the back end with the HTML view
» Helps in bundling HTML code into reusable modules/layouts
» Adds basic conditionals & iterations / loops to your HTML. HTML does not support “If -Else” or “for”
loops.
Many Javascript Template Engines are available : Jade, Handlebars, Hogan, EJS, etc.
12. Slide 12 www.edureka.co/mastering-node-jsSlide 12
Express JS : Jade
Jade is a simple Javascript Template engine for writing HTML. No config has to be set up for ExpressJS
since it is the default view engine
It supports dynamic code, template inheritance and allows you to compile templates into reusable
functions that can run on server
Example of Jade syntax :
JADE
html
head
title My App Title
body
h1= pageHeaderVar
div.className
p.
some
content
HTML
<html>
<head>
<title> My App Title
</title>
</head>
<body>
<h1>Page Header</h1>
<div class=”className”>
<p>
some
content
</p>
</div>
</body>
</html>
13. Slide 13 www.edureka.co/mastering-node-jsSlide 13
Express JS : A Simple Express Application
var express = require(‘express’);
var app = express();
app.get(‘/’,function(req,res){
res.send(‘Hello World !!”);
})
app.listen(3000,function(){
console.log(‘Express Server listening on port 3000);
})
14. Slide 14 www.edureka.co/mastering-node-jsSlide 14
Job Trends
Salaries for Node.js Developers are already in the $60,000 range and much more.
From the graph below : The number of jobs are skyrocketing.