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

Express

Express is a web server framework built on the HTTP server, allowing users to create their own servers and manage requests and responses. It utilizes middleware for handling incoming requests and supports various HTTP methods for routing. Additionally, Express can be enhanced with tools like nodemon for automatic server restarts and templating engines like EJS for rendering views.

Uploaded by

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

Express

Express is a web server framework built on the HTTP server, allowing users to create their own servers and manage requests and responses. It utilizes middleware for handling incoming requests and supports various HTTP methods for routing. Additionally, Express can be enhanced with tools like nodemon for automatic server restarts and templating engines like EJS for rendering views.

Uploaded by

mdsunrisers
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

What is Express?

Is it a tool, library, runtime, or framework?


Express is a framework.

What Does Express Do?

Express is used to create web servers.


It's an advanced version of the HTTP server (in fact, it's built on the
HTTP server).
Express is a third-party package that we need to install using npm
(Node Package Manager).
We create our own server with Express.
Require Express :
Express returns a function, and when we run it, it gives back an
entirely new object (the app), which represents our web server.
The app object is an instance of the entire application. Think of it as the core of
your web server. Everything in your web server runs through this app object.
Microprocessor
Microprocessors are mini computers.
it will run your low level code in bites formate.(0,1).
microprocessors have small small pins(port) →Eg. 8085(40 pins), 8086.
these single pins act as a Port
port means esi jagha jaha aap apne operations perform kr paa rahe ho.Eg. phone
charging port.
if we want to perform any operations/ application we have to decide and give a
port where your web app will run
esi jagha de sake jaha vo apne instance of the application run kr sake .
There are a few ports in our system that are decided for specific tasks
performed in our system, so we can't change them. (pehle se occupied hote hai).
Generally, 8000, 8080, 5000, 5050, and 3000 refer to the port.
But the default port for the backend is 8080.
Listen is a function that accepts a callback function.
It builds a connection with server 8080 and waits for the request or incoming request to
perform a task.
Middleware
Middleware functions act like an agent that intercepts and responds to every
incoming request. app.use() is used to define middleware that is executed on
every request or a specific path.

app.use([path,] callback)
What it does: It's a method to define middleware functions in Express.
Parameters:
path: Optional. It's the specific route the middleware should run (e.g.,
/middleware). If omitted, it applies to all routes.
callback: A function that is mandatory and runs every time a request
hits the server.
Understanding Request and Response Objects (req and res)
Both req (request) and res (response) are objects.
Whenever a client sends a request, the server stores that request as a req
object.
Similarly, when the server sends a response, it stores it as a res object.
You can log these objects to inspect the incoming request and outgoing
response.
Sending a Response with res.send()
The res.send() method sends a response back to the client.

Specifying Middleware for a Specific Path


You can make middleware execute only when a specific route is matched by
passing a path to app.use().
If the path doesn’t match, this middleware will not be executed.
Routing in Express
Different routes return different responses based on the request made.
Each incoming request generates a distinct response, determined by the route
and the type of HTTP method used.
Types of HTTP Requests (Methods)
To learn more, you can visit MDN’s page on HTTP methods: MDN HTTP
Methods
GET → This method is used to retrieve or fetch data from the server.
POST → Sends data to the server to create/update resources.
PATCH → Applies partial modifications to a resource.
DELETE → Deletes a specified resource.

app.get(path, callback [, callback ...])


path and callback function are mandatory parameters.
This function is used to define a route in the application using the GET
method.
Even if you don’t explicitly add a slash (/) at the end of the path, the slash is
assumed by default.
The slash (/) is the default root path for the application.
GET is also the default method to retrieve data for this route.
Using nodemon to Automatically Restart Server
nodemon is a tool that automatically restarts the server whenever you make
changes in your code. This is useful as it saves you from manually restarting
the server after every change.
npm install nodemon
Path and Query Parameter
Path Parameters:
Path parameters are part of the URL structure.
Example: When you visit reddit.com/r/anything, the /r/anything portion is a
path parameter.
/r/ is the constant part (the subreddit path).
anything (e.g., dogs, cricket, etc.) is the variable part.
When you remove the /r/ from the URL, you get a 404 error (page not found)
because /r/ is a required path that leads to different subreddits.

Why /r/ is important?


/r/ is a constant that identifies a subreddit.
Anything after /r/ is treated as a subreddit name, and the content changes
accordingly.
Here, :subreddit is a path parameter. It captures whatever comes after /r/ and
stores it in req.params.
You can access the subreddit using req.params.subreddit.
Query Parameters:
Query parameters appear after a ? in the URL and are key-value pairs.
Example: When you search something on Wikipedia or Google, the URL may
look like this: localhost:8080/search?search=movie.
The part after ? (i.e., search=movie) is the query string
Templating (ejs)
layout same rehta hai different different routes pr
eak hi route banega and eak hi page banega
this concept is called templating.
eak baar hi dhacha/ structure banao or usye use krte raho.

Mental Model
res.render(view [, locals] [, callback])
Express uses res.render() to display views.
view: the template to be rendered.
what is view?
view is a template/ strucuture .
and template ko krna hota hai show
and show krne ke liye mujhe eak engine ki zarurat padegi (browser engines)
we have different templating engines to show template.

Templating Engines
Engines like EJS, Handlebars, Jade/Pug.
all these are my templating engines and this will help me to show the template
on browser.
But we will use ejs templating engine.

You might also like