SlideShare a Scribd company logo
ExpressJS and REST API.pptx
 Express.js is a web framework for Node.js. It is a
fast, robust and asynchronous in nature.
 ExpressJS is a web application framework that
provides you with a simple API to build websites,
web apps and back ends.
 Install express framework globally to create web
application using node terminal.
npm install –g express
npm install express --save
npm install –g nodemon
var express = require('express')
var app = express()
app.get('/',function(req,res){
res.send("Hello Govardhan")
});
app.listen(2022)
 Routing is made from the word route. It is used to
determine the specific behavior of an application.
 It specifies how an application responds to a client
request to a particular route, URI or path and a
specific HTTP request method (GET, POST, etc.). It
can handle different types of HTTP requests.
app.method(path, handler)
 MVC is the most popular & useful structure for web application and it
describes as
▪ Model – It can handle the database
▪ View – It can handle the client-side web pages
▪ Controller – It can control the request & response of Model & View
ExpressJS and REST API.pptx
 Error handling in Express is done using middleware.
But this middleware has special properties. The error
handling middleware are defined in the same way as
other middleware functions, except that error-
handling functions must have four arguments instead
of three – err, req, res, next.
 For error handling, we have the next(err) function. A
call to this function skips all middleware and matches
us to the next error handler for that route.
var express = require('express');
var app = express();
app.get('/', function(req, res){
//Create an error and pass it to the next function
var err = new Error();
next(err);
});
//An error handling middleware
app.use(function(err, req, res, next) {
res.status(500);
res.send("Oops, Something went wrong.")
});
app.listen(2022);
 Express uses the Debug module to internally log
information about route matching, middleware
functions, application mode, etc.
 To see all internal logs used in Express, set the
DEBUG environment variable to Express:* when
starting the app −
DEBUG = express:* node index.js
 A template engine enables you to use static template files
in your application. At runtime, the template engine
replaces variables in a template file with actual values, and
transforms the template into an HTML file sent to the
client. This approach makes it easier to design an HTML
page.
 Some popular template engines that work with Express
are Pug, Mustache, and EJS.
npm install pug --save
SimplePug.pug
doctype html
html
head
title=title
body
h1(align=alignment)=myHeading
SimplePug.js
var express = require('express');
var app = express();
app.set('view engine','pug');
app.get('/',function(req,res){
res.render('SimplePug',{
title: "Template Engine",
myHeading: "Pug Template",
alignment: "center"
});
});
app.listen(2022,function(){
console.log("Sever Running");
});
 Process manager is a container for applications that
facilitates deployment, provides high availability, and
enables you to manage the application at runtime.
 It is helpful to –
 Restart the app automatically if it crashes.
 Gain insights into runtime performance and resource
consumption.
 Modify settings dynamically to improve performance.
 Control clustering.
ExpressJS and REST API.pptx
 RESTful web services are basically REST
architecture based web services.
 RESTful web services are light-weight, highly
scalable and maintainable and are very commonly
used to create APIs for web-based applications.
 REST – REpresentational State Transfer.
 It is web standard based architecture and uses HTTP
protocol.
 It revolves around resource where every component
is a resource and a resource is accessed by a common
interface using HTTP methods.
 Four HTTP methods are commonly used in REST
based architecture:
1. GET – Provides a read only access to a resource.
2. POST – Used to create a new resource.
3. DELETE – Used to remove a resource.
4. PUT – Used to update an existing resource or create a
new resource.
ExpressJS and REST API.pptx
 Uniform interface constraint defines the interface
between clients and servers.
 Four principles of uniform interface:
1. Identifying Resources
2. Manipulation of Resources through Representations
3. Self-descriptive messages
4. Hypermedia as the Engine of Application State
 Each resource in REST architecture is identified
by its URI (Uniform Resource Identifier).
 The generic URI syntax as shown below:
<protocol>://<service-name>/<ResourceType>/<ResourceID>
Cont…
1. A trailing forward slash (/) should not be included in URIs
2. Forward slash separator (/) must be used to indicate a hierarchical
relationship
3. Hyphens (-) should be used to improve the readability of URIs
4. Underscores (_) should not be used in URIs
5. Lowercase letters should be preferred in URI paths
6. File extensions should not be included in URIs
7. Should the endpoint name be plural nouns

More Related Content

ExpressJS and REST API.pptx

  • 2.  Express.js is a web framework for Node.js. It is a fast, robust and asynchronous in nature.  ExpressJS is a web application framework that provides you with a simple API to build websites, web apps and back ends.
  • 3.  Install express framework globally to create web application using node terminal. npm install –g express npm install express --save npm install –g nodemon
  • 4. var express = require('express') var app = express() app.get('/',function(req,res){ res.send("Hello Govardhan") }); app.listen(2022)
  • 5.  Routing is made from the word route. It is used to determine the specific behavior of an application.  It specifies how an application responds to a client request to a particular route, URI or path and a specific HTTP request method (GET, POST, etc.). It can handle different types of HTTP requests. app.method(path, handler)
  • 6.  MVC is the most popular & useful structure for web application and it describes as ▪ Model – It can handle the database ▪ View – It can handle the client-side web pages ▪ Controller – It can control the request & response of Model & View
  • 8.  Error handling in Express is done using middleware. But this middleware has special properties. The error handling middleware are defined in the same way as other middleware functions, except that error- handling functions must have four arguments instead of three – err, req, res, next.  For error handling, we have the next(err) function. A call to this function skips all middleware and matches us to the next error handler for that route.
  • 9. var express = require('express'); var app = express(); app.get('/', function(req, res){ //Create an error and pass it to the next function var err = new Error(); next(err); }); //An error handling middleware app.use(function(err, req, res, next) { res.status(500); res.send("Oops, Something went wrong.") }); app.listen(2022);
  • 10.  Express uses the Debug module to internally log information about route matching, middleware functions, application mode, etc.  To see all internal logs used in Express, set the DEBUG environment variable to Express:* when starting the app − DEBUG = express:* node index.js
  • 11.  A template engine enables you to use static template files in your application. At runtime, the template engine replaces variables in a template file with actual values, and transforms the template into an HTML file sent to the client. This approach makes it easier to design an HTML page.  Some popular template engines that work with Express are Pug, Mustache, and EJS. npm install pug --save
  • 12. SimplePug.pug doctype html html head title=title body h1(align=alignment)=myHeading SimplePug.js var express = require('express'); var app = express(); app.set('view engine','pug'); app.get('/',function(req,res){ res.render('SimplePug',{ title: "Template Engine", myHeading: "Pug Template", alignment: "center" }); }); app.listen(2022,function(){ console.log("Sever Running"); });
  • 13.  Process manager is a container for applications that facilitates deployment, provides high availability, and enables you to manage the application at runtime.  It is helpful to –  Restart the app automatically if it crashes.  Gain insights into runtime performance and resource consumption.  Modify settings dynamically to improve performance.  Control clustering.
  • 15.  RESTful web services are basically REST architecture based web services.  RESTful web services are light-weight, highly scalable and maintainable and are very commonly used to create APIs for web-based applications.
  • 16.  REST – REpresentational State Transfer.  It is web standard based architecture and uses HTTP protocol.  It revolves around resource where every component is a resource and a resource is accessed by a common interface using HTTP methods.
  • 17.  Four HTTP methods are commonly used in REST based architecture: 1. GET – Provides a read only access to a resource. 2. POST – Used to create a new resource. 3. DELETE – Used to remove a resource. 4. PUT – Used to update an existing resource or create a new resource.
  • 19.  Uniform interface constraint defines the interface between clients and servers.  Four principles of uniform interface: 1. Identifying Resources 2. Manipulation of Resources through Representations 3. Self-descriptive messages 4. Hypermedia as the Engine of Application State
  • 20.  Each resource in REST architecture is identified by its URI (Uniform Resource Identifier).  The generic URI syntax as shown below: <protocol>://<service-name>/<ResourceType>/<ResourceID> Cont…
  • 21. 1. A trailing forward slash (/) should not be included in URIs 2. Forward slash separator (/) must be used to indicate a hierarchical relationship 3. Hyphens (-) should be used to improve the readability of URIs 4. Underscores (_) should not be used in URIs 5. Lowercase letters should be preferred in URI paths 6. File extensions should not be included in URIs 7. Should the endpoint name be plural nouns