0% found this document useful (0 votes)
3 views3 pages

Express Notes

This document provides a guide on setting up a basic Express server that responds to requests on the root route. It explains how to install nodemon, create routes, implement middleware, use cookies, and set up EJS as a view engine for rendering dynamic content. Additionally, it covers dynamic routing in Express, allowing for variable paths in the URL while maintaining a consistent route structure.

Uploaded by

fataniyaayush
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Express Notes

This document provides a guide on setting up a basic Express server that responds to requests on the root route. It explains how to install nodemon, create routes, implement middleware, use cookies, and set up EJS as a view engine for rendering dynamic content. Additionally, it covers dynamic routing in Express, allowing for variable paths in the URL while maintaining a consistent route structure.

Uploaded by

fataniyaayush
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Making server and run it on / route on browser . . .

const express = require('express')


const app = express()

[Link]('/',function(req, res){
[Link]('hello ayush...')
})

[Link](3000)

explanation :

here, first line indicates that we import express in our file so that now we can use functionalities
to use functionalities in second line we call express() function and store it in the const app variable
when express() function Is called we can than use the express’s features

to install nodemon in our folder :

to install nodemon in your project first open terminal in your code editor.
Than type the command like below :

 npm i -g nodemon

than press enter key and it will be install in your folder globally.
It means you can use nodemon anywhere in your folder(globally).

creating routes in express :

route means anything after domain name. example :

[Link]/profile
[Link]/signIn
[Link]/home/products

to create route follow below syntax :

[Link](route, requestHandler{
// statements / logic
})
[Link](3000)

explanation :

here, route is a path/route name. it is enclosed in single of double quotes


requestHandler is a middleware. It is one function that can have parameters or not… . if we want that something run/perform before our
request/response is performed than it is achieved through middleware.

Middleware : jab bhi server request accept karta hai waha se route ke beech pahuchne tak agar aap us request ko beech me rokte ho and
kuch perform karte ho, to ye element middleware kehlata hai.

Client request server

[Link](3000) is used to run the server. Here 3000 is a port number. We can use any port number but 3000 is recommended
example :

[Link]('/',function(req, res){
[Link]('hello ayush...')
})

Explanation :

here we create / route which is the root of all routes means it shows the home page when we run it…
here [Link] is used to print(send) some text on page

to create middleware( some task that is performed before response in going from server to client browser ) follow below code :

[Link](function(req, res, next){


statements
})

Explanation :
Here this code is written before main code of we can say that just after where we declare some variables like express, app, etc.
Here all parameters(req, res, next) of function are compulsory

Cookie :

To use cookie we need to first install ‘cookie-parser’ package from npm.


Syntax :
npm i cookie-parser

[Link]([Link]())
[Link]([Link]({ extended : true })

when we submit our form (send our data from browser to server) than these data first transmit into blob format and this blob will be send to
server. This blob format is not directly readable Than we need to handle that this blob format is in convert into readable format.

Setting up ejs for ejs pages from npm

1) To install write below command in terminal in your folder:

npm i ejs

2) set up ejs as a view engine

Step 1: create a folder named ‘views’


Step 2: in that folder make the file that have ‘.ejs’ extension.
Step 3: there is html code inside that file.

We can decide that which file will render at some route by using this ejs feature.
To use this follow below code :

[Link](‘view engine’ , ’ejs’)


[Link](“/” , function(req, res) {
[Link](“chacha”)
})

Explanation :
We need to follow some syntax for this.
Here [Link] is used to render/show ejs pages.
Here [Link] code is says that at / route chacha page will be render/show.
Here [Link] is used for render chacha page.

Now if you want to perform some calculations in ejs file than follow below syntax :
<%= content %>

Here at content we can perform some calculations like below :


<%= 4+4 %>

It display answer 8 on the browser screen


If we perform same operation in html file that the browser displays it as it is means no calculation will be performed.

Dynamic routing in express :

if you want to make route dynamic than just put colon before route name (between / and route name).
here dynamic means only certain part will change other part is same. Complete meaning is explained below :

routes :

/profile/Ayush
/profile/mahi
/profile/shobhna

Here /profile route is same in all routes but routes after /profile are different. It means profile round will not changed while routes after /profile
will changed, it means /Ayush, /mahi, and /shobhna routes are dynamic.

You might also like