SlideShare a Scribd company logo
EXPRESS JS
Prepared By:
Mrs. Bareen Shaikh
Assistant Professor
Computer Science
MIT ACSC Alandi(D)
Pune
Express JS content
▪ 9.1 REST
▪ 9.2 Introduction to Express JS
▪ 9.3 Routing, Responding
▪ 9.4 Configuration
▪ 9.5Views
▪ 9.6 Receiving Data
▪ 9.7 Error Handling
REST
▪ REST stands for REpresentational StateTransfer.
▪ REST was first introduced by Roy Fielding in 2000.
▪ REST is web standards based architecture using
HTTP Protocol.
▪ It revolves around resource where every
component is a resource .
▪ A resource is accessed by a common interface using
HTTP standard methods.
▪ A REST Server simply provides access to resources
and REST client accesses and modifies the
resources using HTTP protocol.
▪ REST uses various representation to represent a
resource like text, JSON, XML but JSON is the most
popular one.
RESTful Web Services
▪ Web services based on REST Architecture are known as
RESTful web services.
▪ These webservices uses HTTP methods to implement
the concept of REST architecture.
▪ A RESTful web service usually defines a URI, Uniform
Resource Identifier a service, which provides resource
representation such as JSON and set of HTTP Methods.
▪ HTTP methods
▪ Following four HTTP methods are commonly used in
REST based architecture.
 GET −This is used to provide a read only access to a resource.
 PUT −This is used to create a new resource.
 DELETE −This is used to remove a resource.
 POST −This is used to update a existing resource or create a
new resource.
RESTful Architecture(Example
of Customer Service)
RESTful Architecture
Overview
Introduction to Express JS
▪ Express was developed byTJ Holowaychuk .
▪ Express is a minimal and flexible Node.js web
application framework .
▪ It provides a robust set of features to develop web
and mobile applications.
▪ Following are the core features of Express
framework:
 Allows to set up middlewares to respond to HTTP
Requests.
 Defines a routing table which is used to perform different
action based on HTTP Method and URL.
 Allows to dynamically render HTML Pages based on
passing arguments to templates.
Introduction to Express JS
▪ It is maintained by the Node.js foundation and
numerous open source contributors.
▪ It facilitates a rapid development of Node based
Web applications.
▪ Express provides a minimal interface to build
our applications.
▪ It provides us the tools that are required to build
our app.
▪ It is flexible as there are numerous modules
available on npm, which can be directly plugged
into Express.
Middleware
▪ Express is a routing and middleware web
framework that has minimal functionality of its
own.
▪ An Express application is essentially a series of
middleware function calls.
▪ Middleware functions are functions that have
access to the request object (req), the response
object (res), and the next middleware function
in the application’s request-response cycle.
▪ The next middleware function is commonly
denoted by a variable named next.
Using Middleware
▪ Middleware functions can perform the following tasks:
 Execute any code.
 Make changes to the request and the response objects.
 End the request-response cycle.
 Call the next middleware function in the stack.
 If the current middleware function does not end the request-
response cycle, it must call next() to pass control to the next
middleware function. Otherwise, the request will be left
hanging.
▪ An Express application can use the following types of
middleware:
 Application-level middleware
 Router-level middleware
 Error-handling middleware
 Built-in middleware
 Third-party middleware
Installing Express
▪ Firstly, install the Express framework using npm so that it can be
used to create web application using node terminal.
 $ npm install express --save
▪ Above command saves installation locally in node_modules
directory and creates a directory express inside node_modules.
▪ Following more modules should be install along with express:
 body-parser -This is a node.js middleware for handling JSON,
Raw,Text and URL encoded form data.
 $ npm install body-parser --save
 cookie-parser - Parse Cookie header and populate req.cookies
with an object keyed by the cookie names.
 $ npm install cookie-parser --save
 multer -This is a node.js middleware for handling
multipart/form-data.
 $ npm install multer --save
Hello World Example
var express = require('express');
var app = express();
app.get('/', function(req, res){
res.send("Hello world!");
});
app.listen(4000);
 Save the file, in terminal type the following.
$node hello.js
 This will start the server.
 Open browser and open http://localhost:4000.
Explanation of example
▪ app.get(route, callback)
 This function tells what to do when a get request at
the given route is called.
 Callback function has 2 parameters, request(req)
and response(res).
 The request object(req) represents the HTTP request
and has properties for the request query string,
parameters, body, HTTP headers, etc.
 The response object represents the HTTP response
that the Express app sends when it receives an HTTP
request.
Explanation of example
▪ res.send()
 send() function takes an object as input
 It sends this to the requesting client.
 In above example sending the string "HelloWorld!".
▪ app.listen(port, [host], [backlog], [callback]])
 listen() binds and listens for connections on the
specified host and port.
 port is the required parameter here, remaining
parameter are optional.
 backlog is the maximum number of queued pending
connections.
 callback is an asynchronous function that is called when
the server starts listening for requests.
ExpressJS Routing
▪ Following function is used to define routes for an
application
 app.method(path, handler)
▪ METHOD can be applied any one of the HTTP
verbs – get, set, put, delete.
▪ An alternate method also exists, which
executes independent of the request type.
▪ Path is the route at which request will run.
▪ Handler is a callback function that executes
when a matching request type is found on the
relevant route.
ExpressJS Routing Example
var express = require('express');
var app = express();
app.get('/', function(req, res){
res.send("Hello world!");
});
app.get('/hello', function(req, res){
res.send(“routed to /hello page");
});
app.listen(4000);
 Open browser and open localhost:4000/hello
 The server receives a get request at route "/hello”
 Express app executes the callback function attached to this
route
ExpressJS Routing Example
var express = require('express');
var app = express();
app.get('/', function(req, res){
res.send("Hello world!");
});
var server = app.listen(4000, function () {
var host = server.address().address
var port = server.address().port
console.log("%s : %s", host, port)
}) ;

More Related Content

ExpressJS-Introduction.pdf

  • 1. EXPRESS JS Prepared By: Mrs. Bareen Shaikh Assistant Professor Computer Science MIT ACSC Alandi(D) Pune
  • 2. Express JS content ▪ 9.1 REST ▪ 9.2 Introduction to Express JS ▪ 9.3 Routing, Responding ▪ 9.4 Configuration ▪ 9.5Views ▪ 9.6 Receiving Data ▪ 9.7 Error Handling
  • 3. REST ▪ REST stands for REpresentational StateTransfer. ▪ REST was first introduced by Roy Fielding in 2000. ▪ REST is web standards based architecture using HTTP Protocol. ▪ It revolves around resource where every component is a resource . ▪ A resource is accessed by a common interface using HTTP standard methods. ▪ A REST Server simply provides access to resources and REST client accesses and modifies the resources using HTTP protocol. ▪ REST uses various representation to represent a resource like text, JSON, XML but JSON is the most popular one.
  • 4. RESTful Web Services ▪ Web services based on REST Architecture are known as RESTful web services. ▪ These webservices uses HTTP methods to implement the concept of REST architecture. ▪ A RESTful web service usually defines a URI, Uniform Resource Identifier a service, which provides resource representation such as JSON and set of HTTP Methods. ▪ HTTP methods ▪ Following four HTTP methods are commonly used in REST based architecture.  GET −This is used to provide a read only access to a resource.  PUT −This is used to create a new resource.  DELETE −This is used to remove a resource.  POST −This is used to update a existing resource or create a new resource.
  • 7. Introduction to Express JS ▪ Express was developed byTJ Holowaychuk . ▪ Express is a minimal and flexible Node.js web application framework . ▪ It provides a robust set of features to develop web and mobile applications. ▪ Following are the core features of Express framework:  Allows to set up middlewares to respond to HTTP Requests.  Defines a routing table which is used to perform different action based on HTTP Method and URL.  Allows to dynamically render HTML Pages based on passing arguments to templates.
  • 8. Introduction to Express JS ▪ It is maintained by the Node.js foundation and numerous open source contributors. ▪ It facilitates a rapid development of Node based Web applications. ▪ Express provides a minimal interface to build our applications. ▪ It provides us the tools that are required to build our app. ▪ It is flexible as there are numerous modules available on npm, which can be directly plugged into Express.
  • 9. Middleware ▪ Express is a routing and middleware web framework that has minimal functionality of its own. ▪ An Express application is essentially a series of middleware function calls. ▪ Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. ▪ The next middleware function is commonly denoted by a variable named next.
  • 10. Using Middleware ▪ Middleware functions can perform the following tasks:  Execute any code.  Make changes to the request and the response objects.  End the request-response cycle.  Call the next middleware function in the stack.  If the current middleware function does not end the request- response cycle, it must call next() to pass control to the next middleware function. Otherwise, the request will be left hanging. ▪ An Express application can use the following types of middleware:  Application-level middleware  Router-level middleware  Error-handling middleware  Built-in middleware  Third-party middleware
  • 11. Installing Express ▪ Firstly, install the Express framework using npm so that it can be used to create web application using node terminal.  $ npm install express --save ▪ Above command saves installation locally in node_modules directory and creates a directory express inside node_modules. ▪ Following more modules should be install along with express:  body-parser -This is a node.js middleware for handling JSON, Raw,Text and URL encoded form data.  $ npm install body-parser --save  cookie-parser - Parse Cookie header and populate req.cookies with an object keyed by the cookie names.  $ npm install cookie-parser --save  multer -This is a node.js middleware for handling multipart/form-data.  $ npm install multer --save
  • 12. Hello World Example var express = require('express'); var app = express(); app.get('/', function(req, res){ res.send("Hello world!"); }); app.listen(4000);  Save the file, in terminal type the following. $node hello.js  This will start the server.  Open browser and open http://localhost:4000.
  • 13. Explanation of example ▪ app.get(route, callback)  This function tells what to do when a get request at the given route is called.  Callback function has 2 parameters, request(req) and response(res).  The request object(req) represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers, etc.  The response object represents the HTTP response that the Express app sends when it receives an HTTP request.
  • 14. Explanation of example ▪ res.send()  send() function takes an object as input  It sends this to the requesting client.  In above example sending the string "HelloWorld!". ▪ app.listen(port, [host], [backlog], [callback]])  listen() binds and listens for connections on the specified host and port.  port is the required parameter here, remaining parameter are optional.  backlog is the maximum number of queued pending connections.  callback is an asynchronous function that is called when the server starts listening for requests.
  • 15. ExpressJS Routing ▪ Following function is used to define routes for an application  app.method(path, handler) ▪ METHOD can be applied any one of the HTTP verbs – get, set, put, delete. ▪ An alternate method also exists, which executes independent of the request type. ▪ Path is the route at which request will run. ▪ Handler is a callback function that executes when a matching request type is found on the relevant route.
  • 16. ExpressJS Routing Example var express = require('express'); var app = express(); app.get('/', function(req, res){ res.send("Hello world!"); }); app.get('/hello', function(req, res){ res.send(“routed to /hello page"); }); app.listen(4000);  Open browser and open localhost:4000/hello  The server receives a get request at route "/hello”  Express app executes the callback function attached to this route
  • 17. ExpressJS Routing Example var express = require('express'); var app = express(); app.get('/', function(req, res){ res.send("Hello world!"); }); var server = app.listen(4000, function () { var host = server.address().address var port = server.address().port console.log("%s : %s", host, port) }) ;