HsbsnzmzmMzmmzmzmsmznndnxnxnxxkachaohhichhdhjdoqhdodpuddqddffj f jodi kd onwoncowjowjofowbiohivhihhiboookhndhhuhrgrjrjennjdfjfnfwbohevrrfxbmmkkicerrjjrtkgjlfbvjjjbbfb HsbsnzmzmMzmmzmzmsmznndnxnxnxxkachaohhichhdhjdoqhdodpuddqddffj f jodi kd onwoncowjowjofowbiohivhihhiboookhndhhuhrgrjrjennjdfjfnfwbohe...
HsbsnzmzmMzmmzmzmsmznndnxnxnxxkachaohhichhdhjdoqhdodpuddqddffj f jodi kd onwoncowjowjofowbiohivhihhiboookhndhhuhrgrjrjennjdfjfnfwbohevrrfxbmmkkicerrjjrtkgjlfbvjjjbbfb HsbsnzmzmMzmmzmzmsmznndnxnxnxxkachaohhichhdhjdoqhdodpuddqddffj f jodi kd onwoncowjowjofowbiohivhihhiboookhndhhuhrgrjrjennjdfjfnfwbohevrrfxbmmkkicerrjjrtkgjlfbvjjjbbfb
Size: 1.25 MB
Language: en
Added: Aug 25, 2024
Slides: 19 pages
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
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