Professional Documents
Culture Documents
Full Stack - Unit 4 Part 1
Full Stack - Unit 4 Part 1
EXPRESS: Implementing Express in Node.js - Configuring routes - Using Request and Response objects.
Angular: Typescript - Angular Components - Expressions - Data binding - Built-in directives.
Configuring Routes
Implementing Routes
Applying Parameters in Routes
• Setting Headers
• Setting the Status
• Sending Response
• Sending JSON Responses
• Sending Files
• Sending a Download Response
• Redirecting the Response
EXPRESS JS: Fast, unopinionated, minimalist web framework for Node.js
Express is a lightweight module that wraps the functionality of the Node.js http module in a simple-to- use interface
Express is a node js web application framework that provides features for building web and mobile apps.
It is used to build a single page, multipage, and hybrid web application.
It is a layer built on the top of the Node js that helps manage servers and routes.
Version:
Express 4.18.1
Express 5.0 beta
Minimalism & Flexibility - minimal & flexible Node.js web application framework for web & mobile app
Robust Routing System - allows you to easily map URLs to specific functions or controllers
Middleware Support - request handler that has access to the application's request-response cycle.
Template Engine Integration – build dynamic content to make the application more organized &
maintainable.
2 Marks
import the express module and create an instance of express or var express = require('express');
var app = express()
Starting the Express Server
app.listen(8080);
Configuring Express Settings app.enable('trust proxy');
app.disable('strict routing');
app.set('view engine', 'pug');
1. Implementing Express in Node.js
U plz write this in Paragraph | For understanding it’s in tabular form
2. Install
Install Express using npm: npm install express
Express
Create a file (e.g., app.js)
app.js and implement // File Name: app.js
the following Express application:
Output Screen
Coding Without Explanation
2. Configuring Routes in Express JS
Routing in Express JS is the process of specifying how an application replies to a client request to a
specific endpoint or URL.
To run a function upon request for a certain route, route handlers are used.
Syntax : app.method(path, [callback . . .], callback)
Two parts are there in defining routes:
1. HTTP Request Method:
The HTTP request method determines the type of operation the client is requesting.
Different HTTP request methods are used for different purposes:
GET : retrieving data from the server.
POST : submitting data to be processed to a specified resource.
PUT : updating a resource on the server.
DELETE : requesting that a resource be removed or deleted.
Example :
}); Output
Response Object(res)
The res object represents the HTTP response that an Express app sends when it receives an HTTP request.
It provides methods for sending a response, setting headers, and managing the response body.
Description Method Definition
How do you set the HTTP status for the response and list the different statuses?
To set the status response, use the status(number) method where the number parameter is the HTTP response status
Example :
res.status(200); // OK
res.status(300); // Redirection
res.status(400); // Bad Request
res.status(401); // Unauthorized
res.status(403); // Forbidden
res.status(500); // Server Error
-----------------