Unit 4 - Express & Angular
Unit 4 - Express & Angular
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) 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
Template This option allows you to add inline HTML to define the look of the component
templateUrl This option allows you to import an external template file rather than inline HTML.
Styles This option allows you to add inline CSS to your component.
stylesUrls This option allows you to import an array of external CSS stylesheet(s
This is an array of dependency injection providers.
viewProviders It allows you to import and use Angular services that provide application functionality
such as HTTP communications.
Example :
The following section describe the most important aspects of the Angular framework and how they contribute
to make Angular a great JavaScript framework.
• Angular applications are modular and organized into cohesive units called modules.
• Modules encapsulate related components, directives, pipes, and services.
Modules
• // app.module.ts
o Import {Component} from 'angular2/core';
o Export class App{}
• Directives are JavaScript classes with metadata that defines the structure and behavior.
Directives • Directives provide the majority of UI functionality for Angular applications.
• Three major types of directives: Components , structural and attrinbutes.
Data Binding —the process of linking data from a component with what is displayed in a web page
• a process in which a component defines dependencies on other components.
Dependency • When the code is initialized, the dependent component is made available for access
Injection within the component. Angular applications make heavy use of dependency injection.
• Common use for dependency injection is consuming services.
Services in Angular are singleton objects that provide functionality and features to multiple
Services
parts of the application, promoting code reusability.
• AngularJS Expressions are used to bind data to HTML elements, allowing dynamic content rendering.
• Angular evaluates expressions and then can dynamically add the results to a web page.
• AngularJS expressions can be written inside double braces: {{ expression }}.
• AngularJS expressions can also be written inside a directive: ng-bind="expression".
• AngularJS will resolve the expression, and return the result exactly where the expression is written.
• AngularJS expressions are enclosed within double curly braces and can contain variables, literals,
operators, and method calls.
• Example: {{ 5 + 5 }}
{{ firstName + " " + lastName }}
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
// Without the ng-app directive, HTML will display the expression as it is, without solving it.
</body>
</html>
</div>
In the above example:
• Interpolation ({{ }}): Displays the user's name directly in the template.
• Property Binding ([ ]): Sets the value of the input field to the user's name.
• Event Binding (( )): Calls the onButtonClick method when the button is clicked.
• Attribute Binding ([ ]): Dynamically sets the image source based on the user's age.
• Class Binding ([ ] with class): Dynamically applies the 'adult' or 'child' class based on the user's age.
• Style Binding ([ ] with style): Dynamically sets the text color based on the user's age.
• Two-Way Binding with ngModel ([(ngModel)]): Binds the input field value to both the user's name property
and the input value simultaneously.
+++++++++++++
Angular Built-in Directives
• Angular Directives are a combination of Angular template markup and supporting TypeScript code.
• It can be expressed through HTML attributes, element names, or CSS classes.
• The TypeScript directive code outlines the data and behavior for HTML elements associated with the
directive.
• During compilation, the Angular compiler processes and compiles all directives in the template DOM.
• Angular directives are markers on a DOM element that tell Angular to manipulate the element or
transform its behavior.
Description Explanation
Components Directives A directive with a template : Attribute:
Structural Directives A directive that manipulates elements in the DOM
Directive Description Example
Iterates over a collection
ngFor and renders content for <li *ngFor="let item of items">{{ item }}</li>
each item.
Conditionally renders
ngSwitch content based on matching <div [ngSwitch]="selectedOption"> ... </div>
cases.
ngSwitchCase Used within an ngSwitch <p *ngSwitchCase="'Option 1'"> You selected Option 1</p>
& block to define specific <p *ngSwitchCase="'Option 2'"> You selected Option 2 </p>
ngSwitchDefault cases. <p *ngSwitchDefault> Select an option </p>
// app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `
<h1 *ngIf="isUserLoggedIn">Welcome, {{ userName }}!</h1>
<p *ngIf="!isUserLoggedIn">Please log in to access the content.</p> ‘,
styleUrls: ['./app.component.css']
})
export class AppComponent {
isUserLoggedIn: boolean = true;
userName: string = 'John Doe';
}
Attribute Directives A directive that manipulates the appearance and behavior of a DOM element
Directive Description Example
Implements two-way data binding,
ngModel allowing changes in the UI to <input [(ngModel)]="userName" />
update the model and vice versa.
Creates a top-level form instance
and exposes form-related directives,
ngForm <form #myForm="ngForm" ngSubmit)="submitForm"> ... </form>
allowing form validation and
submission.
Binds CSS styles dynamically based
ngStyle <p [ngStyle] = " {'color': textColor, 'font-size': fontSize } "> Styled Text </p>
on expression evaluation.
******