EXPRESS JS Flashcards
(5 cards)
what is express
summary : libray and framework are both set of pre written codes but libray are used to perfrom some specific task whereas framewrok is used to give structure for developing web appilciation exprees is node js based framework that is used build web appilcation it used for 4 thing s1) listen to incoming request 2) parse the data in suitable js object 3)sending/matching response according to the routes 4) sending suitable response
libray vs framework : both are set of pre written code but in case of libray it is used to perform a specific task whereas in framwork it is used to give structure to our program how will it be written control is more in libray than framwork as framework gives the struture for developing our software applications
express js : its a nodejs based web application framework that help us to make website/ web apps, used for server side programing
4 use case of express :
1) listen to upcoming request
2) parse the data
When you send any kind of data from the client (browser, mobile app, etc.),
it can come in different formats:
JSON data (from API requests),URL encoded data (from HTML forms),Multipart form data (for file uploads),Cookies
Problem:
This raw data is not automatically ready to use inside Node.js. as they are npt objects or in form in which js can use them
It usually comes as raw streams (basically just bytes of data).
What Express does (with middleware like express.json(), express.urlencoded()):
✅ It reads the raw data
✅ It understands the format (JSON, URL encoded, etc.)
✅ It converts it into a JavaScript object
✅ Then Node.js code can easily work with it like normal JavaScript variables
3) match response with routes : routes are different page navigation in website like google.com/search , /home,/projects etc these are routes in programing
4) sending suitable response
express installation
summary: npm init ot install package.json if not there then npm install express to install express locally then index,,js file reqiure express run the express appilcation and listen for a specific port to see the output first run the node index.js then go to browers and type localhost:portNo mentioned
first make sure there is package.json if not then npm init (insilize it) then
use command : npm install express
once done to to make sever with express we do the following
1) we require express : const express=require(“express”);
2) store express() function in app variable to make it an object
const app=express();
3)defining port : let port =3000; //it can be 8080 also
port is the point where data transfer(request>response) work
4)an listen object in app to create sever
syntax of listen app : app.listen(portNo,function);
code: app.listen(port,()=>{
console.log(“app is listening”)};
)
run the indexjs file : node index,js
then in brower type “localhost:3000” to see the sever working
explanation
with express varible you require the express libray in your current code file
with app varible you create an express application , express() returns an express appilcation object which contian various method like routes , listen etc so that we can handle sever side things
with port vaiarble we are defining the port at which our browser will interact/comunicate with our local system
Your computer is a building.
It has many doors (ports): door 8080, door 3000, door 5000, etc.
When a browser wants to talk to your server, it knocks on a door (port) and says:
“Hey! Are you running a server here?”
If your Express server is listening on port 8080, it will open the door and reply.
let port = 8080;
You are saying:
“I want my server to be available at door number 8080.”
with app.listen its a fucntion that lets you wait for the request by browser/ client at a specific port its like saying:”Hey Express, please sit at this port number and wait for anyone who tries to connect!”
it take two parameters 1) port no and 2) is callback function to print your message but note The callback function is optional!
If you don’t give a callback, the server will still start — but you won’t get any message printed.
app.listen(3000);
handling request
app.use is a method in app object thatr is used to listen any request either get,post etc and excutes the function inside it everytime the request is recevied
it is used to handle request
app.use((req,res)=>{
console.log(“request recevied “);
})
note : another argument that we pass is next agrument to pass it next handler as if next is not written then it will get stuck
app.use((req,res,next)=>{
console.log(“request recevied “);
next();
})
Every time a request comes, this function runs.
It logs “A new request received!” to your console.
Then calls next() to move forward (otherwise request will hang).
app.use it used handle any type of request : get ,post etc
sending response
req.send is used to send response to the request send by the client
note: the https request we get are in text based so that the backend is written in any language(python,c++,js) could understand it
in node/js this text based is converted to object form through parapasing by express and once request handled by us adn response is send in form of text or json representaion
req.send can support various response likem string,html,array,objects ,boolean
when response is string or html tags then content type send is text/html
when response is array or objects then content type send is json represenation
note: there cannot be two res.send in a single request handling i.e app.use
ques: even if i use two different app.use with single res.send its still not printing the second res.send why fint it out
ans : for each path there can be only one response and app.use have same response for all path so if one response is send through app.use no other response will be accepted/send as each path can single response only therefore for there cannot be 2 response if request generated for same path .there either you send html file ,string,array or Boolean only one response allowed per path (paths are google.com,google.com/search etc
routing
its a process to handle diiferent path request send by the client
syntax:
app.get(“/path name”,(req,res)=>{
res.send(“your message”);
})
app.get method is used to send response to specific path mentioned in the path name it has two arguments
1) path : its the path at which we want a specific response note : “/” is the default path
2) callback function : it tell what to excutes if there is request on the specified path
app.use vs app.get
in app.use response (res.send()) to any path is the same where using app.get we can send response to specific paths
important
to deal with client typing path that does not exist we can prepare for it by “” path as name “” path mean send response to anyother path requested by the client other than the above mentioned/coded path
synatx
app.get(“*”,(req,res)={
res.send(response to send);
});
example :
app.get(“*”,(req,res)=>{
res.send(“path requested does not exist”);
});
its not running the code is correct according to chat gpt its showing new type error figure it out
new concept get vs post request
get : when we want to send data from sever like pdf,information etc
post: when we are sending data to sever like uploading photo,pdf,video based on that we want a response
for post type we write its syntax is
app.post(“/”,(req,res){
res.send();
}) // this handle the post request from the client