ExpressJS Basics
ExpressJS Basics
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
Introduction
• Express.JS is a popular and powerful web application framework for Node.js that allows
• It is a minimalist framework that provides a variety of features and functions for developing
• Express.js provides a simple and flexible routing system, middleware functions, templates,
2
3/21/2024
• Express.js is built on top of Node.js, which means that it can take advantage of all the features
• Express.js can be used to build SPAs, which are web applications that provide a seamless
• Mobile applications:
• Express.js can be used to build mobile applications that use web technologies like HTML,
• RESTful APIs:
• Express.js is commonly used to build RESTful APIs that can be used by other applications
3
3/21/2024
Features of Express
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
• Step - 3:
4
3/21/2024
Step - 5:
Start the app by following command.
node app.js
行业PPT模板http ://www.1p p t.co m/hang ye/
• Open your terminal or command prompt and run the following command to install the
• Step - 2:
• Navigate to the Application Directory then Install the dependencies specified in the
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
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.
• A route function is derived from one of the HTTP methods and is attached to an express class instance.
• app.METHOD(PATH,CALLBACK)
• This method instructs the server to execute the next CALLBACK function and send an HTTPMETHOD
7
3/21/2024
• 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
• 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.
8
3/21/2024
• 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
9
3/21/2024
• 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
10
3/21/2024
• 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
• To construct routes with route parameters, simply enter the route arguments in the path of the route
11
3/21/2024
• 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
12
3/21/2024
• A query string is, to put it simply, the portion of a URL (Uniform Resource Locator) that comes just
• 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
• These can be used to organize web pages by information or data, which is especially helpful with REST
APIs.
https://example.com/profile/kce
13
3/21/2024
http://localhost:3000/profile/john/india
Express Router
• Express Router is a middleware component in Express.js that allows you to group routes in a modular
• 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.
14
3/21/2024
• 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
• 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
• 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
15
3/21/2024
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.
16