UNIT-4(MST)
1.RESTful Web Services:REST or Representational State Transfer is an architectural style that
can be applied to web services to create and enhance properties like performance, scalability,
and modifiability. RESTful web services are generally highly scalable, light, and maintainable
and are used to create APIs for web based applications.
REST became popular due to the following reasons:
1. It allows web applications built using different programming languages to communicate with
each other. Also, web applications may reside in different environments, like on Windows, or for
example, Linux.
2. Mobile devices have become more popular than desktops. Using REST, you don’t need to
worry about the underlying layer for the device. Therefore, it saves the amount of effort it would
take to code applications on mobiles to talk with normal web applications.
3. Modern applications have to be made compatible with the Cloud. As Cloud-based
architectures work using the REST principle, it makes sense for web services to be programmed
using the REST service-based architecture.
RESTful Architecture:
1. Division of State and Functionality: State and functionality are divided into distributed
resources. This is because every resource has to be accessible via normal HTTP commands.
That means a user should be able to issue the GET request to get a file, issue the POST or
PUT request to put a file on the server, or issue the DELETE request to delete a file from the
server.
2. Stateless, Layered, Caching-Support, Client/Server Architecture: A type of architecture
where the web browser acts as the client, and the web server acts as the server hosting the
application, is called a client/server architecture. The state of the application should not be
maintained by REST. The architecture should also be layered, meaning that there can be
intermediate servers between the client and the end server. It should also be able to implement
a well-managed caching mechanism.
Principles of RESTful applications:
1. URI Resource Identification: A RESTful web service should have a set of resources that can
be used to select targets of interactions with clients. These resources can be identified by URI
(Uniform Resource Identifiers). The URIs provide a global addressing space and help with
service discovery.
2. Uniform Interface: Resources should have a uniform or fixed set of operations, such as PUT,
GET, POST, and DELETE operations. This is a key principle that differentiates between a REST
web service and a non-REST web service.
3. Self-Descriptive Messages: As resources are decoupled from their representation, content
can be accessed through a large number of formats like HTML, PDF, JPEG, XML, plain text,
JSON, etc. The metadata of the resource can be used for various purposes like control caching,
detecting transmission errors, finding the appropriate representation format, and performing
authentication or access control.
4. Use of Hyperlinks for State Interactions: In REST, interactions with a resource are stateless,
that is, request messages are self-contained. So explicit state transfer concept is used to
provide stateful interactions. URI rewriting, cookies, and form fields can be used to implement
the exchange of state. A state can also be embedded in response messages and can be used
to point to valid future states of interaction.
Advantages of RESTful web services:
1. Speed: As there is no strict specification, RESTful web services are faster as compared to
SOAP.
It also consumes fewer resources and bandwidth.
2. Compatible with SOAP: RESTful web services are compatible with SOAP, which can be used
as the implementation.
3. Language and Platform Independency: RESTful web services can be written in any
programming language and can be used on any platform.
4. Supports Various Data Formats: It permits the use of several data formats like HTML, XML,
Plain Text, JSON, etc.
Example :
Here is an example of a simple RESTful service that allows a client to create, read, update, and
delete
(CRUD) a resource :
// GET /resource/123
// Returns the state of the resource with ID 123
app.get(’/resource/:id’, function(req, res) {
var id = req.params.id;
var resource = findResourceById(id);
res.json(resource);
});
// POST /resource
// Creates a new resource with the state specified in the request body
app.post(’/resource’, function(req, res) {
var resource = req.body;
var id = createResource(resource);
res.json({ id: id });
});
// PUT /resource/123
// Updates the state of the resource with ID 123 with the state specified in
the request body
app.put(’/resource/:id’, function(req, res) {
var id = req.params.id;
var resource = req.body;
updateResource(id, resource);
res.sendStatus(200);
});
// Deletes the resource with ID 123
app.delete(’/resource/:id’, function(req, res) {
var id = req.params.id;
deleteResource(id);
res.sendStatus(200);
});
2.Explain the concept of RESTful APIs in Express.
ANS:Understanding the concept of RESTful APIs in Express JS:
REST Architecture: REST, which stands for Representational State Transfer, is an architectural
style for designing networked applications.
Resource-Based: RESTful APIs in ExpressJS are designed around resources, which are
identified by unique URLs. These resources represent the entities (e.g., users, products,
articles) that the API manipulates.
HTTP Methods: RESTful APIs use standard HTTP methods to perform CRUD (Create, Read,
Update, Delete) operations on resources:
GET: Retrieves data from a resource.
POST: Creates a new resource.
PUT: Updates an existing resource.
DELETE: Deletes a resource.
Example: Below is the basic example of the RESTful API in ExpressJS
Javascript
// server.js
const express = require(’express’);
const app = express();
const PORT = 3000;
// To define the sample data
let books = [
{
id: 1,
title: ’The Great Gatsby’,
author: ’F. Scott Fitzgerald’
},
{
id: 2,
title: ’To Kill a Mockingbird’,
author: ’Harper Lee’
},
];
// Define routes for handling GET requests
app.get(’/api/books’,
(req, res) => {
res.json(books);
});
app.get(’/api/books/:id’,
(req, res) => {
const id =
parseInt(req.params.id);
const book =
books.find(book => book.id === id);
if (book) {
res.json(book);
} else {
res.status(404)
.json({ message: ’Book not found’ });
}
});
app.listen(PORT,
() => {
console.log(‘Server is running on port ${PORT}‘);
});
3.Explain two way binding?Explain with an example program?
Ans:
Two-way data binding is a concept where the UI (view) and the model (component or data
source) are synchronized in both directions:
● If data in the component changes → the view updates automatically.
● If the user updates input in the view → the data in the component is updated.
Example in Angular:
// app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
})
export class AppComponent {
message: string = 'Hello, Angular!';
}
<!-- app.component.html -->
<div>
<h2>Two-Way Data Binding Example</h2>
<input [(ngModel)]="message" placeholder="Enter message here">
<p>You typed: {{ message }}</p>
</div>