node js express: steps to create restful web app
TRANSCRIPT
www.edureka.co/mastering-node-js
View Mastering Node.js course details at www.edureka.co/mastering-node-js
Node JS Express - Steps to create Restful Web App
Slide 2 www.edureka.co/mastering-node-js
LIVE Online Class
Class Recording in LMS
24/7 Post Class Support
Module Wise Quiz
Project Work
Verifiable Certificate
Course Features
Slide 3 www.edureka.co/mastering-node-js
Objectives
At the end of the session you will be able to learn:
NPM
Node.js Express
Templates in Express
Creating Restful Web App
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
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
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
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 wasfar greater than any risk in adopting a new technology
2. They re-engineered their mobile app to run on Node.js where allthe front end code gets executed on back-end
3. “We rely on services all over the world,” says Almaer (V.P MobileArchitecture) “We do not control all of those services. Nodeallows 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
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
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
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
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.
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 :
JADEhtml
headtitle My App Title
body h1= pageHeaderVardiv.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>
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);
})
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.
Slide 15 www.edureka.co/mastering-node-js
DEMO
Slide 16 www.edureka.co/mastering-node-js
Course Topics
→ Module 7
» Forks, Spawns and the Process Module
→ Module 8
» Testing in Node.js
→ Module 9
» Node.js in the Tech World
→ Module 1
» Introduction to Objects in Javascript & Node.js
→ Module 2
» Modules / Packages
→ Module 3
» Events & Streams
→ Module 4
» Network Communication & Web Technology in Node.js
→ Module 5
» Building a Web Application
→ Module 6
» Real-time Communication
Slide 17 www.edureka.co/mastering-node-js
Questions
Slide 18 www.edureka.co/mastering-node-js