U4 02 Express JS

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 24

20ADC07

FULL STACK DEVELOPMENT

UNIT-IV

Express JS
What is Express JS?

Fast
https://expressjs.com/
unopinionated,

minimalist

web framework

for Node.js

$ npm install express


Template Engine in Express JS

 Template engine helps us to create an HTML template with minimal code.

Also, it can inject data into HTML template at client side and produce the final

HTML.

 A Template Engine enables you to use static template files in your application.

 At runtime, the template engine replaces variables in a template file with actual

values, and transforms the template into an HTML file sent to the client.

 This approach makes it easier to design an HTML page.


Template Engine in Express JS

Simple Input Output Produced by Engine


Template Engine in Express JS
Following is a list of some popular template engines Express.js:
 EJS Embedded JavaScript Template
 Pug (formerly known as jade)
 mustache
 dust
 atpl
 eco
 ect
 ejs
 haml
 haml-coffee
 handlebars
 hogan
EJS Template Engine in Express JS

 Template engine makes you able to use static template files in your

application. To render template files you have to set the following

application setting properties:

 Views: It specifies a directory where the template files are located.

For example: app.set('views', './views').

 view engine: It specifies the template engine that you use. For example, to

use the EJS template engine: app.set('view engine', ‘ejs').


EJS Template Engine in Express JS
 Initialize a new Node project in the folder by running npm init -y in the
terminal, then to install Express and EJS, run:

npm install express ejs

 create an app.js file and a views folder in the root folder. Inside the views
folder, create folder pages.

Note: Click Here to Access the implementation in Drive.


EJS Template Engine in Express JS
Pug Template Engine in Express JS
Install pug
 Execute the following command to install pug template engine:
npm install pug

 Create a file named index.pug

file inside views folder and write

the following pug template in it:


Pug Template Engine in Express JS
File: server.js
Template Engine in Express JS

Advantages of Template engine in Node.js

 Improves developer's productivity.

 Improves readability and maintainability.

 Faster performance.

 Maximizes client side processing.

 Single template for multiple pages.

 Templates can be accessed from CDN (Content Delivery Network).


Defining Routes

 Route paths, in combination with a request method, define the endpoints at

which requests can be made.

Route paths

 Route paths, in combination with a request method, define the endpoints at

which requests can be made. Route paths can be strings, string patterns, or

regular expressions.
Defining Routes
This route path will match requests to the root route, /.
app.get('/', (req, res) => {
res.send('root')
})
This route path will match requests to /about.
app.get('/about', (req, res) => {
res.send('about')
})
This route path will match requests to /random.text.
app.get('/random.text', (req, res) => {
res.send('random.text')
})
Route paths based on string patterns.
This route path will match acd and abcd.
app.get('/ab?cd', (req, res) => {
res.send('ab?cd')
})
This route path will match abcd, abbcd, abbbcd, and so on.
app.get('/ab+cd', (req, res) => {
res.send('ab+cd')
})
This route path will match abcd, abxcd, abRANDOMcd, ab123cd, and so on.
app.get('/ab*cd', (req, res) => {
res.send('ab*cd')
})
Route paths based on regular expressions
This route path will match anything with an “a” in it.
app.get(/a/, (req, res) => {
res.send('/a/')
})
This route path will match butterfly and dragonfly, but not butterflyman,
dragonflyman, and so on.
app.get(/.*fly$/, (req, res) => {
res.send('/.*fly$/')
})
Route parameters

 Route parameters are named URL segments that are used to capture the values

specified at their position in the URL.

 The captured values are populated in the req.params object, with the name of the

route parameter specified in the path as their respective keys.

Route path: /users/:userId/books/:bookId

Request URL: http://localhost:3000/users/34/books/8989

req.params: { "userId": "34", "bookId": "8989" }


Response Methods
Method Description
res.download() Prompt a file to be downloaded.

res.end() End the response process.

res.json() Send a JSON response.

res.jsonp() Send a JSON response with JSONP support.

res.redirect() Redirect a request.

res.render() Render a view template.

res.send() Send a response of various types.

res.sendFile() Send a file as an octet stream.

res.sendStatus() Set the response status code and send its string representation as the response body.
Defining Routes-index.html

Note: Click Here to Access the implementation in Drive.


Defining Routes- index.js
Middleware in Express
The middleware in node.js is a function that will have all the access
for requesting an object, responding to an object, and moving to the next
middleware function in the application request-response cycle.
Middleware in Express

Middleware functions can perform the following tasks:

Execute any code.

Make changes to the request and the response objects.

End the request-response cycle.

Call the next middleware in the stack.


Middleware in Express

An Express application can use the following types of middleware.

 Application-level middleware

 Router-level middleware

 Error-handling middleware

 Built-in middleware

 Third-party middleware
Middleware in Express
The app.use() function is used to mount the specified middleware
function(s) at the path which is being specified. It is mostly used to set up
middleware for your application.
Syntax:
app.use(path, callback)

Note: next(), executes the middleware succeeding the current middleware


Middleware/Custom Middleware in Express

Note: Click Here to Access the implementation in Drive.

You might also like