CH-2.2.1 (1).pptx hisnsmmzmznznNNNMamMamam

renunitin9919 19 views 19 slides Aug 25, 2024
Slide 1
Slide 1 of 19
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19

About This Presentation

HsbsnzmzmMzmmzmzmsmznndnxnxnxxkachaohhichhdhjdoqhdodpuddqddffj f jodi kd onwoncowjowjofowbiohivhihhiboookhndhhuhrgrjrjennjdfjfnfwbohevrrfxbmmkkicerrjjrtkgjlfbvjjjbbfb HsbsnzmzmMzmmzmzmsmznndnxnxnxxkachaohhichhdhjdoqhdodpuddqddffj f jodi kd onwoncowjowjofowbiohivhihhiboookhndhhuhrgrjrjennjdfjfnfwbohe...


Slide Content

DISCOVER . LEARN . EMPOWER UNIT-1 UNIVERSITY INSTITUTE OF COMPUTING MASTER OF COMPUTER APPLICATIONS Backend Technologies 23CAH-705 1

Express Introduction to Express, Express Generator, Express session , Passport Backend Technologies CO Number Title Level CO3 Understand the working of Git to upload the created project 2.1.3, 3.1.1 CO4 Apply the CRUD operations of MongoDB in the development of website 3.1.1, 3.4.3 Course Outcome 2

Express Express is a popular web application framework for Node.js, designed for building web applications and APIs. It provides a robust set of features to develop web and mobile applications, making it an essential tool for developers . Here’s an in-depth look at Express : Key Features 1. Middleware : Middleware functions are functions that have access to the request object ( req ), the response object (res), and the next middleware function in the application’s request-response cycle. Middleware can execute code, modify the request and response objects, end the request-response cycle, and call the next middleware function. 3

2. Routing : Express has a powerful routing system that allows you to define routes for different HTTP methods and URLs. Routes can handle different types of requests and are capable of processing parameters, queries, and request bodies. 3. Template Engines : Express supports various template engines, allowing you to generate HTML dynamically. Commonly used template engines include Pug, EJS, and Handlebars. 4. Static Files : Express can serve static files, such as HTML, CSS, and JavaScript files. This is typically done using the built-in express.static middleware . 4

5. Error Handling : Express provides a robust mechanism for error handling, allowing you to define custom error-handling middleware. 6. Scalability : Express applications are easily scalable, allowing you to build single-page, multi-page, or hybrid web applications. 5

Setting Up an Express Application 1. Installation : To install Express, you need to have Node.js and npm installed. Then, you can create a new project and install Express: mkdir myapp cd myapp npm init -y npm install express 6

2. Basic Application Structure : A simple Express application might look like this: const express = require('express'); const app = express(); const port = 3000; app.get ('/', ( req , res) => { res.send ('Hello World!'); }); app.listen (port, () => { console.log(`App listening at http://localhost:${port}`); }); 7

Middleware Middleware functions can be used for various purposes, such as logging, authentication, parsing request bodies, and more. 1. Built-in Middleware : Express includes several built-in middleware functions, such as express.json () and express.urlencoded (). app.use ( express.json ()); app.use ( express.urlencoded ({ extended: true })); 8

2. Third-Party Middleware : You can use third-party middleware for additional functionality, such as morgan for logging and cookie-parser for cookie handling. const morgan = require(' morgan '); const cookieParser = require('cookie-parser'); app.use ( morgan ('tiny')); app.use ( cookieParser ()); 9

3. Custom Middleware : You can also define your own middleware functions. app.use (( req , res, next) => { console.log('Time:', Date.now ()); next(); }); 10

Routing Express provides a flexible routing system that allows you to define routes for different HTTP methods and paths. 1. Basic Routing : Define routes using app.get , app.post , app.put , app.delete , etc. app.get ('/user', ( req , res) => { res.send ('GET request to the user'); }); app.post ('/user', ( req , res) => { res.send ('POST request to the user'); }); 11

2. Route Parameters : Route parameters are named URL segments that act as placeholders for values in the URL. app.get ('/user/:id', ( req , res) => { res.send (`User ID: ${req.params.id}`); }); 12

3. Route Handlers : You can define multiple callback functions for a single route. app.get ('/example', ( req , res, next) => { console.log('First handler'); next(); }, ( req , res) => { res.send ('Second handler'); }); 13

4. Router Object : Use the express.Router class to create modular, mountable route handlers. const express = require('express'); const router = express.Router (); router.get ('/', ( req , res) => { res.send ('Home Page'); }); router.get ('/about', ( req , res) => { res.send ('About Page'); }); app.use ('/', router); 14

Error Handling Express provides a simple mechanism for handling errors. 1. Error-Handling Middleware : Define error-handling middleware functions to catch and handle errors. app.use ((err, req , res, next) => { console.error ( err.stack ); res.status (500).send('Something broke!'); }); 15

Template Engines Express supports various template engines to generate dynamic HTML. 1. Pug Example : Install and use Pug with Express: Bash npm install pug Javascript app.set ('view engine', 'pug'); app.get ('/template', ( req , res) => { res.render ('index', { title: 'Hey', message: 'Hello there!' }); }); 16

Serving Static Files Express can serve static files using the express.static middleware. app.use ( express.static ('public')); 17

References https://www.amazon.in/Full-Stack-JavaScript-Development-MEAN/dp/0992461251 https://books.google.co.in/books/about/Full_Stack_React_TypeScript_and_Node.html?id=uUMQEAAAQBAJ&redir_esc=y https://hub.packtpub.com/web-development-react-and-bootstrap/ https://www.oreilly.com/library/view/pro-mern-stack/9781484243916/ 18

THANK YOU 19
Tags