Unit-2 - Express Framework
Unit-2 - Express Framework
Unit: 2
Express Framework
Praveen Tomar
Course Details (Asst. Professor)
(B. Tech. 6th Sem)
CSE-AI Department
Study how to design and build static as well as dynamic webpages and
interactive web applications
Students examine advanced topics like Angular, nodejs, Mongodb for web
applications.
Also examine Express framework for interactive web applications that use rich
user interfaces .
CO3 : Apply the knowledge of Typescript that are vital in understanding angular js.
CO4 : Analyze build and develop single page application using client-side programming.
CO5 : Understand the impact of web designing by database connectivity with Mongodb
CO.K PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
CO1 2 2 2 3 3 - - - - - - -
CO2 3 2 3 2 3 - - - - - - -
CO3 3 2 3 2 3 - - - - - - -
CO4 3 2 3 2 3 - - - - - - -
CO5 3 2 3 3 3 - - - - - - -
CO1 3 - - -
CO2 3 3 - -
CO3 3 3 - -
CO4 3 3 - -
CO5 3 3 - -
Program Educational
PEOs Description
Objectives (PEOs)
To have an excellent scientific and engineering breadth so as to comprehend,
PEOs analyze, design and provide sustainable solutions for real-life problems using state-
of-the-art technologies.
To have life-long learning for up-skilling and re-skilling for successful professional
PEOs career as engineer, scientist, entrepreneur and bureaucrat for betterment of society.
• https://youtu.be/BLl32FvcdVM
• https://youtu.be/v9ejT8FO-7I?list=PLrhzvIcii6GNjpARdnO4ueTUAVR9eMBpc
• https://youtu.be/VGLjQuEQgkI?list=PLt4nG7RVVk1h9lxOYSOGI9pcP3I5oblbx
MEAN .js is designed for the developers who want to design the dynamic
website and web application using Mean.js.
Advantages of Express.js
It is simple and lightweight software. It is not heavy to get installed in the machine
and make the application running.
It is easy to customize and configure as we can see it provides the flexibility that we
require.
It is a better choice for creating the API as when the application requires various APIs
to communicate with different people then the Express.js is a good option..
You should install some other important modules along with express.
body-parser: This is a node.js middleware for handling JSON, Raw, Text and URL
encoded form data.
body-parser: This is a node.js middleware for handling JSON, Raw, Text and URL
encoded form data.
COMMAND:
cookie-parser:
It is used to parse Cookie header and populate req.cookies with an object keyed by
the cookie names.
COMMAND:
COMMAND:
npm install multer --save
COMMAND:
npm install multer --save
Postman:
Postman is a standalone software testing API (Application Programming Interface)
platform to build, test, design, modify, and document APIs.
It is a simple Graphic User Interface for sending and viewing HTTP requests and
responses.
While using Postman, for testing purposes, one doesn't need to write any HTTP
client network code. Instead, we build test suites called collections and let
Postman interact with the API.
In this tool, nearly any functionality that any developer may need is embedded.
This tool has the ability to make various types of HTTP requests like GET, POST,
PUT, PATCH, and convert the API to code for languages like JavaScript and Python.
Step-2: For downloading the app for Windows, click on the download
button and select the particular version. I opted for the 64-bit version.
If you are using a 32-bit OS, you can choose the 32 bit, as shown in the
above image.
Step-3: You can check the download progress on the bottom left if you
are using the Chrome browser. Once the .exe file is downloaded, you
need to install the application, as shown in the below image.
Step-5: Create your account with all the required details, or you can
also signup with Google, as shown in the image.
Step-6: After signing in, select the workspace tools as per your
requirement, and then click on, continue to get the startup screen.
Enter the below mentioned key-value pairs in which Key =url and Initial Value =
http://restapi.demoqa.com.
Then select the Update button and close the Manage Environment window. Now
you have an environment variable with the name 'url', and the value of the variable
is the http://restapi.demoqa.com.
Note: By default Express.js searches all the views in the views folder under the root folder. you
can also set to another folder using views property in express. For example:
app.set('views','MyViews').
<!DOCTYPE html>
<html>
<head>
<title>A simple pug example</title>
</head>
<body>
<h1>This page is produced by pug template engine</h1>
<p>some paragraph here..</p>
</body>
</html>
Express.js
can be used with any template engine. Let's take an example to deploy how pug template creates HTML page
dynamically.
03/27/2024 SWETA PANDEY WEB DEVELOPMEN 68
T USING MEAN STACK Unit 2
Pug Template
Create a file named index.pug file inside views folder and write the following pug template in it:
doctype html
html
head
title A simple pug example
body
h1 This page is produced by pug template engine
p some paragraph here..
File: server.js
var express = require('express');
var app = express();
//set view engine
app.set("view engine","pug")
app.get('/', function (req, res) {
res.render('view.pug', index);
res.render('index');
});
var server = app.listen(5000, function () {
console.log('Node server is running..');
});
03/27/2024 SWETA PANDEY WEB DEVELOPMEN 69
T USING MEAN STACK Unit 2
Routing
Express.js Routing
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.
Routing in ExpressJS is used to subdivide and organize the web application into multiple mini-
applications each having its own functionality.
It provides more functionality by subdividing the web application rather than including all of the
functionality on a single page.
These mini-applications combine together to form a web application. Each route in Express responds
to a client request to a particular route/endpoint and an HTTP request method (GET, POST, PUT,
DELETE, UPDATE and so on).
Each route basically refers to the different URLs in the website. So when a URL.
03/27/2024 SWETA PANDEY WEB DEVELOPMEN 70
T USING MEAN STACK Unit 2
ExpressJS - Routing
To use the dynamic routes, we SHOULD provide different types of routes. Using dynamic routes allows us
to pass parameters and process based on them.
• In this topic, the students will learn about the methods of express.
What are URL binding and middleware funtion. Student will learn
regarding sttic files and express session .
GET and POST both are two common HTTP requests used for
building REST API's.
GET requests are used to send only limited amount of data
because data is sent into header while POST requests are used to
send large amount of data because data is sent in the body.
Express.js facilitates you to handle GET and POST requests using
the instance of express.
Get method facilitates you to send only limited amount of data
because data is sent in the header. It is not secure because data is
visible in URL bar.
1.<html>
2.<body>
3.<form action="http://127.0.0.1:8000/get_example2" method="GET">
4.First Name: <input type="text" name="first_name"/> <br/>
5.Last Name: <input type="text" name="last_name"/><br/>
6.<input type="submit" value="Submit"/>
7.</form>
8.</body>
9.</html>
03/27/2024 SWETA PANDEY WEB DEVELOPMEN 75
T USING MEAN STACK Unit 2
GET Method Cont……
File: get_example.js
Output:
File: Index.html
1.<html>
2.<body>
3.<form action="http://127.0.0.1:8000/process_post" method="POST">
4.First Name: <input type="text" name="first_name"> <br>
5.Last Name: <input type="text" name="last_name">
6.<input type="submit" value="Submit">
7.</form>
8.</body>
9.</html>
03/27/2024 SWETA PANDEY WEB DEVELOPMEN 80
T USING MEAN STACK Unit 2
POST Method Cont…….
File: post_example1.js
OUTPUT:
Note: In the above picture, you can see that entries are not visible in the URL bar unlike GET method.
Application-level middleware
Router-level middleware
Error-handling middleware
Built-in middleware
Third-party middleware
03/27/2024 SWETA PANDEY WEB DEVELOPMEN 85
T USING MEAN STACK Unit 2
Express.js Middleware Function Conti....
simple example of a middleware function in action −
The above middleware is called for every request on the server. So after every request, we will get the following message in the
console −
A new request received at 1467267512545
• html
• head
• body
• h3 Testing static file serving:
• img(src = "/testimage.jpg", alt = "Testing Image
OUTPUT:
Whenever we make a request from the same client again, we will have their session information stored with us
(given that the server was not restarted).
when a user visits the site, it creates a new session for the user and assigns them a cookie.
Next time the user comes, the cookie is checked and the page_view session variable is updated accordingly.
Topic : REST full API’s, FORM data in Express, document modeling with
Mongoose.
• In this topic, the students will gain , The idea of REST full API. FORM
data usedin Express and document modeling with Mongoose will
be cover.
After importing the body parser and multer, we will use the body-parser for
parsing json and x-www-form-urlencoded header requests, while we will
use multer for parsing multipart/form-data.
At console; it will show you the body of your request as a JavaScript object
REST API is a software that allows two apps to communicate with one
another over the internet and through numerous devices.
GET - Get command is used to request data from the server, but mainly this
method is used to read data
PATCH - This command is used to update, change or replace the data
POST - The post method is used to create new or to edit already existing
data
Delete - This delete command is used to delete the data completely from
the server
A request is sent by the client in the form of a JSON file, and with the help of an
HTTP request which gets a patch post and delete, it will go to the server first
then, the server sends back the response to the client in the form of a message
to tell what happened to your request.
Step 1: First, open your editor. Now open your terminal and write a command npm init -y
Write a few lines of code in the first line. We will import the express packages
Step 5: Then we will write a second argument as a callback to tell the API is working, now lets run this code to
check our API is working or node
To run this code, we will write a command node.
Step 6: As our code is running fine, let's open our search engine and write localhost:4000, this will not run,
localhost:4000/simplilearn
Syntax:
Model.create()
Parameters: The Model.create() method accepts three parameters:
npm init
Step 2: After creating the NodeJS application, Install the required module
using the following command:
{
name: 'Rahul',
orderCount: 5,
_id: new ObjectId("6304e68407a431f560473ac2"),
__v: 0
}
DATABASE:
1) Which of the following command is used to check the current version of NPM?
a. nmp --ver
b. npm --version
c. npm help
d. None of the above
2) Which of the following method requests that the server accept the data enclosed in the request to
modify an existing object identified by the URI?
e. GET
f. DELETE
g. PUT
h. POST
a. Terse
b. DRY
c. Express
d. Jade
4. Which of the following function is used to specify what to do when a get request at the given route is
called?
e. app.get(route, callback)
f. get(route, callback)
g. js.get(route, callback)
h. fun.get(route, callback
• https://youtu.be/rI4kdGLaUiQ?list=PL6n9fhu94yhUbctIoxoVTrklN3LMwTCmd
• https://youtu.be/v9ejT8FO-7I?list=PLrhzvIcii6GNjpARdnO4ueTUAVR9eMBpc
• https://youtu.be/VGLjQuEQgkI?list=PLt4nG7RVVk1h9lxOYSOGI9pcP3I5oblbx
a. Larry wall
b. Rich Hickey
c. TJ Holowaychuk
d. Rob Pike
2) Which of the following are the core features of the Express framework?
i. November 2010
j. November 2011
k. December 2010
l. December 2011
SWETA PANDEY WEB DEVELOPMENT USING MEAN STACK
03/27/2024 113
Unit 2
MCQ (End of Unit)
4) Where are the captured values populated regarding the route parameters?
a. req.data
b. app.locals
c. req.params
d. All of the above
5) How is it possible to create chainable route handlers for a route path in Express.js?
e. Using app.route()
f. Using app.routes()
g. Using app.router()
h. Using app.routing()
1. What is Express.js?
2. What are some distinctive features of Express?
3. Is Express.js front-end or backend framework?
4. Why do we use Express.js?
5. What is the difference between Express.js and Node.js?
6. Write a code to get post a query in Express.js
7. What is the difference between Express and Django
8. How can you deal with error handling in Express.js? Explain with an example
9. Write the code to start serving static files in Express.js.
10.What is Middleware in Express.js? What are the different types of
Middleware?
03/27/2024 SWETA PANDEY WEB DEVELOPMEN 115
T USING MEAN STACK Unit 2
Expected Questions for University Exam