0% found this document useful (0 votes)
18 views

ExpressJS Basics

Uploaded by

21S121 KAVINR
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

ExpressJS Basics

Uploaded by

21S121 KAVINR
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

3/21/2024

ExpressJS
G e t t i n g s t a r t e d w i t h E x p r e s s . j s

PART 01
Introduction
to Express.js
Let us begin our journey through express.js

1
3/21/2024

ExpressJS

Introduction to Express
You will learn
• What is Express JS?
• Features of Express
• Installing Express
• Creating Express Server

行业PPT模板http ://www.1p p t.co m/hang ye/

Introduction

• Express.JS is a popular and powerful web application framework for Node.js that allows

developers to easily build robust and scalable web applications.

• It is a minimalist framework that provides a variety of features and functions for developing

web applications quickly and efficiently.

• Express.js provides a simple and flexible routing system, middleware functions, templates,

and error-handling features, making it easy to build complex web applications.

行业PPT模板http ://www.1p p t.co m/hang ye/

2
3/21/2024

What is Express JS?

• Express.js is built on top of Node.js, which means that it can take advantage of all the features

and functions of Node.js, including its speed and scalability.

行业PPT模板http ://www.1p p t.co m/hang ye/

What is Express.js Used for?


• Single-page applications (SPAs):

• Express.js can be used to build SPAs, which are web applications that provide a seamless

user experience by loading all the necessary resources on a single page.

• Mobile applications:

• Express.js can be used to build mobile applications that use web technologies like HTML,

CSS, and JavaScript.

• RESTful APIs:

• Express.js is commonly used to build RESTful APIs that can be used by other applications

to access and manipulate data.


行业PPT模板http ://www.1p p t.co m/hang ye/

3
3/21/2024

Features of Express

Routing Error Handling


Express.js provides a simple robust error handling
and flexible routing system features that help
that allows developers to developers handle errors
map HTTP requests to and exceptions in their
specific functions applications

Middleware Security
Middleware functions can protect against common
be used to add security vulnerabilities like
functionality to your Templates cross-site scripting (XSS)
application, such as It supports popular template engines and cross-site request
authentication, error like EJS, Handlebars, and Pug forgery (CSRF).
handling, and more.
行业PPT模板http://www.1ppt.com/hangye/

Installing Express
• Step - 1:

• Creating a directory for our project and making that our working directory.

• Step - 2:

• Using the npm init command to create a package.json file for our project.

• This command describes all the dependencies of our project. The file will be updated when

adding further dependencies to the project.

• Step - 3:

• npm install express –save

行业PPT模板http ://www.1p p t.co m/hang ye/

4
3/21/2024

Creating Express Server


Step - 4:
Create a file app.js

Step - 5:
Start the app by following command.
node app.js
行业PPT模板http ://www.1p p t.co m/hang ye/

Another Way of Creating Express App


• Step - 1:

• Open your terminal or command prompt and run the following command to install the

Express application generator globally on your system:

• npx express-generator my-express-app

• Step - 2:

• Navigate to the Application Directory then Install the dependencies specified in the

package.json file by running: npm install

• Start your Express application by running: npm start

行业PPT模板http ://www.1p p t.co m/hang ye/

5
3/21/2024

PART 02
Introduction to
Routing in
Expressjs
Let us begin our journey through express.js

Routing

Routing
You will learnin Expressjs
• What is Routing?
• Routing Methods
• Route Paths
• Route Parameters
• Route Handlers

行业PPT模板http ://www.1p p t.co m/hang ye/

6
3/21/2024

What is Routing?

• The word "routing" describes the process of deciding how an application will respond to a client request

for a particular path and HTTP request type. (GET, POST, and so on).

• The application essentially "listens" for requests that match the route (endpoint) and method(s)

specified, and when it finds a match, it executes the callback function specified.

• Routing, put simply, is deciding which process is launched whenever a user navigates to a specific

URL.

行业PPT模板http ://www.1p p t.co m/hang ye/

What are the Routing Methods?

• A route function is derived from one of the HTTP methods and is attached to an express class instance.

• Routing methods are defined as follows:

• app.METHOD(PATH,CALLBACK)

• This method instructs the server to execute the next CALLBACK function and send an HTTPMETHOD

request if the user navigates to PATH.

行业PPT模板http ://www.1p p t.co m/hang ye/

7
3/21/2024

What are the Routing Methods?

• The following verbs are used in place of METHOD:

• Get: To process GET requests (that is, to request/GET data from a given resource).

• Post: To send info to a server to update an old resource or create a new resource.

• Put: A request method used to send data to a server to create or update a resource. Unlike POST

requests, PUT requests are idempotent, meaning multiple identical requests have no additional effect.

It's crucial to note that POST requests may cause side effects if called more than once, so they should

be made only once.

• Delete: To remove a specific resource from the database.

行业PPT模板http ://www.1p p t.co m/hang ye/

What are the Routing Methods?

• Head: Like a GET request, the HEAD method asks that the target resource transfer a representation of

its state, but without the representation data enclosed in the response body.

• Connect: The connect method instructs the intermediary to create a TCP/IP tunnel to the origin server

specified by the request destination. It is frequently used to secure communications via one or more

HTTP proxies.

• Options: The options method asks the destination resource to send the HTTP methods it supports.

This can be used to test a web server's functionality by requesting * instead of a particular resource.

行业PPT模板http ://www.1p p t.co m/hang ye/

8
3/21/2024

What are the Routing Methods?

• Trace: The trace method asks that the received request be transferred to the target resource in the

response body. A client can then see what modifications or additions (if any) have been made by

intermediaries.

• Patch: This function requests that the target resource modify its state based on the partial update

specified in the request's representation. This can save bandwidth by updating a portion of a file or

document without transferring the complete thing.

行业PPT模板http ://www.1p p t.co m/hang ye/

What are the Routing Methods?

行业PPT模板http ://www.1p p t.co m/hang ye/

9
3/21/2024

What are the Routing Methods?


• app.all() is a special routing technique that loads middleware functions at a path for all HTTP request
methods.

行业PPT模板http ://www.1p p t.co m/hang ye/

What are Route Paths?

• The destinations at which requests can be made are defined by route paths in conjunction with

request methods.

• Strings, string patterns, and regular expressions can all be used as route paths

• string-based route paths:

行业PPT模板http ://www.1p p t.co m/hang ye/

10
3/21/2024

What are Route Paths?

• string pattern-based route paths

• Route routes based on regular expressions

行业PPT模板http ://www.1p p t.co m/hang ye/

What are Route Parameters?

• Route parameters are URL segments that are used to capture the values provided in the URL at their

position. The recorded values are stored in the req.params object, and their keys are the names of the

route parameters provided in the path.

Request URL: http://localhost:3000/users/1/books/22


Route path: /users/:userId/books/:bookId
req.params: { "userId": "1", "bookId": "22" }

• To construct routes with route parameters, simply enter the route arguments in the path of the route

行业PPT模板http ://www.1p p t.co m/hang ye/

11
3/21/2024

What are Route Handlers?

• To handle a request, you can provide numerous callback functions that act as middleware. The only

difference is that these callbacks may use the next('route') to skip the remaining route callbacks. This

mechanism can be used to impose preconditions on a particular route and then transfer the control to

next routes if there is no cause to continue with the current route.

• A route can be handled by a single callback method.

行业PPT模板http ://www.1p p t.co m/hang ye/

What are Route Handlers?


A route can be handled by more than one callback method. (make sure you specify the next
object).

行业PPT模板http ://www.1p p t.co m/hang ye/

12
3/21/2024

Query Parameter in Express

• A query string is, to put it simply, the portion of a URL (Uniform Resource Locator) that comes just

after the question mark (?). http://localhost:3000/profile?username=johndoe&age=30

• It is intended to use the URL to transmit the server a little quantity of data because express query

params are preferable for flattened data (nonhierarchical), and also URL has a size limit

行业PPT模板http ://www.1p p t.co m/hang ye/

Getting Route Parameter Using req. params in


Express
• Placing information within the URL, which Express simply refers to as route parameters /profile, is

another typical method of structuring URLs in any web application.

• These can be used to organize web pages by information or data, which is especially helpful with REST

APIs.

https://example.com/profile/kce

行业PPT模板http ://www.1p p t.co m/hang ye/

13
3/21/2024

Multiple Route Parameters

http://localhost:3000/profile/john/india

行业PPT模板http ://www.1p p t.co m/hang ye/

Express Router

• Express Router is a middleware component in Express.js that allows you to group routes in a modular

and organized manner.

• While you can define routes directly on the app object in Express, using Express.Router can help

improve the structure, readability, and maintainability of your code, especially in larger applications.

行业PPT模板http ://www.1p p t.co m/hang ye/

14
3/21/2024

Benefits of Express Router:

• Modularity and Code Organization: Express Router allows you to organize your routes into

separate modules or files based on functionality or resource type, promoting better code organization

and maintainability.

• Middleware Isolation: Each router instance in Express.js has its own middleware stack, enabling you

to apply middleware specific to a set of routes without affecting other routes, thus keeping middleware

isolated and focused.

• Route Prefixing: With Express Router, you can define routes with a common prefix, which is

beneficial for grouping related routes under a common URL prefix. This is particularly useful for

scenarios such as API versioning.


行业PPT模板http ://www.1p p t.co m/hang ye/

Benefits of Express Router:

• Improved Scalability: Managing routes directly on the app object can become unwieldy as the

application grows. Express Router allows you to break down your application into smaller, more

manageable modules, enhancing scalability and making it easier to maintain over time.

• Code Reusability: Routers can be exported and reused across multiple Express applications or

modules, promoting code reuse and ensuring consistency across different parts of the application. This

enhances development efficiency and reduces duplication of code.

行业PPT模板http ://www.1p p t.co m/hang ye/

15
3/21/2024

Creating Express Router


create a router file called "birds.js"

行业PPT模板http ://www.1p p t.co m/hang ye/

Creating Express Router


Then, load the router module in the app:

The app will now be able to handle requests to /birds and /birds/about, as well as
call the date of access middleware function that is specific to the route.

行业PPT模板http ://www.1p p t.co m/hang ye/

16

You might also like