Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Express.js: Building Web Applications with Node.js and Express Routing, Slides of Web Programming and Technologies

Express.js is a popular Node.js framework for building web applications, including single-page, multi-page, and hybrid apps. what Express.js is, how to install and use it, and provides an overview of routing and response methods. It also covers route parameters, route handlers, and the use of Express.Router for modular routing.

What you will learn

  • How do route handlers work in Express.js?
  • What is Express.js and how is it used for web application development?
  • What is Express.Router and how is it used for modular routing?
  • How do you install and use Express.js?
  • What are routes in Express.js and how are they defined?

Typology: Slides

2020/2021

Uploaded on 05/16/2021

hirdesh-kumar-1
hirdesh-kumar-1 🇵🇰

1 / 27

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
EXPRESSJS
FRAMEWORK Dr. Adeel Ansari
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

Download Express.js: Building Web Applications with Node.js and Express Routing and more Slides Web Programming and Technologies in PDF only on Docsity!

EXPRESSJS

FRAMEWORK

Dr. Adeel Ansari

TOPICS TO BE COVERED

ExpressJS Framework

Routing

Web Server with Express

WHAT IS EXPRESSJS?

The MEAN is a free and open-source JavaScript software stack for

building dynamic web sites and web applications which has the

following components;

1) MongoDB - The standard NoSQL database

2) Express.js - The default web applications framework

3) Angular.js - The JavaScript MVC framework used for web

applications

4) Node.js - Framework used for scalable server-side and

networking applications.

The Express.js framework makes it very easy to develop an

application which can be used to handle multiple types of requests

like the GET, PUT, and POST and DELETE requests.

INSTALLING AND USING

EXPRESS

Express gets installed via the Node Package Manager. This can be

done by executing the following line in the command line

The above command requests the Node package manager to

download the required express modules and install them

accordingly.

Let's use our newly installed Express framework and create a

simple "Hello World" application.

npm install express

INSTALLING AND USING

EXPRESS

var express=require('express');

var app=express();

app.get('/',function(req,res)

res.send('Hello World!');

var server=app.listen(3000,function() {});

CODE EXPLANATION:

  1. In our first line of code, we are using the require function to include the "express

module."

  1. Before we can start using the express module, we need to make an object of it.
  2. Here we are creating a callback function. This function will be called whenever

anybody browses to the root of our web application which is http://localhost:.

The callback function will be used to send the string 'Hello World' to the web page.

  1. In the callback function, we are sending the string "Hello World" back to the client.

The 'res' parameter is used to send content back to the web page. This 'res'

parameter is something that is provided by the 'request' module to enable one to

send content back to the web page.

  1. We are then using the listen to function to make our server application listen to

client requests on port no 3000. You can specify any available port over here.

WHAT ARE ROUTES?

Routing determine the way in which an application responds to a client request to a particular

endpoint.

For example, a client can make a GET, POST, PUT or DELETE http request for various URL

such as the ones shown below;

In the above example,

If a GET request is made for the first URL, then the response should ideally be a list of books.

If the GET request is made for the second URL, then the response should ideally be a list of

Students.

So based on the URL which is accessed, a different functionality on the webserver will be

invoked, and accordingly, the response will be sent to the client. This is the concept of

routing.

http://localhost:3000/Books

http://localhost:3000/Students

EXPLANATION

Each route can have one or more handler functions, which are executed

when the route is matched.

The general syntax for a route is shown below

Wherein,

1) app is an instance of the express module

2) METHOD is an HTTP request method (GET, POST, PUT or DELETE)

3) PATH is a path on the server.

4) HANDLER is the function executed when the route is matched.

app.METHOD(PATH, HANDLER)

CODE

var express = require('express');

var app = express();

app.route('/Node').get(function(req,res)

{

res.send("Tutorial on Node");

});

app.route('/Angular').get(function(req,res)

{

res.send("Tutorial on Angular");

});

app.get('/',function(req,res){

res.send('Welcome to Guru99 Tutorials');

}));

APP.ALL METHOD

Express supports methods that correspond to all HTTP request

methods: get, post, and so on. For a full list, see app.METHOD.

There is a special routing method, app.all(), used to load

middleware functions at a path for all HTTP request methods. For

example, the following handler is executed for requests to the route

“/secret” whether using GET, POST, PUT, DELETE, or any other

HTTP request method supported in the http module.

app.all('/secret', function (req, res, next) {

console.log('Accessing the secret section ...’)

next() // pass control to the next handler

})

ROUTE PARAMETERS

Route parameters are named URL segments that are used to

capture the values specified at their position in the URL. The

captured values are populated in the req.params object, with the

name of the route parameter specified in the path as their

respective keys.

Route path: /users/:userId/books/:bookId

Request URL: http://localhost:3000/users/34/books/

req.params: { "userId": "34", "bookId": "8989" }

app.get('/users/:userId/books/:bookId', function (req, res) {

res.send(req.params)

ROUTE HANDLERS

A single callback function can handle a route. For example:

More than one callback function can handle a route (make sure you

specify the next object). For example:

app.get('/example/a', function (req, res) {

res.send('Hello from A!’)

})

app.get('/example/b', function (req, res, next) {

console.log('the response will be sent by the next function ...’)

next()

}, function (req, res) {

res.send('Hello from B!’)

})

ROUTE HANDLERS

An array of callback functions

can handle a route. For

example:

var express = require('express');

var app = express();

var cb0 = function (req, res, next) {

console.log('CB0')

next()

var cb1 = function (req, res, next) {

console.log('CB1')

next()

var cb2 = function (req, res) {

res.send('Hello from C!')

app.get('/example/c', [cb0, cb1, cb2])

var server=app.listen( 3000 ,()=>{});