w7-RESTful apps Flashcards
What is RESTful API?
RESTFul API is an application programming interface that uses HTTP requests to perform the four CRUD operations (Create, Retrieve, Update, Delete) on data.
REST?
REST - representational state transfer
Is it state or stateless?
It is a stateless architecture (sender or receiver retains, i.e. no information) that uses the Web’s existing protocols and technologies.
What does REST architecture consist of?
The REST architecture consists of:
clients servers resources a vocabulary of HTTP operations known as request methods (such as GET, PUT, or DELETE).
Benefits of REST?
-The separation between the client and the server
-The REST API is always independent of the type of platform or language.
--With REST, data produced and consumed is separated from the technologies that facilitate production and consumption. As a result, REST performs well, is highly scalable, simple, and easy to modify and extend.
-Since REST is based on standard HTTP operations, it uses verbs with specific meanings, such as “get” or “delete”, which avoids ambiguity. Resources are assigned individual URIs, adding flexibility.
HTTP methods?
type of action on resources
1.GET method requests data from the resource and should not produce any side effects. E.g. GET /actors returns the list of all actors.
2.POST method requests the server to create a resource in the database, mostly when a web form is submitted. E.g POST /movies creates a new movie where the movie’s property could be in the request’s body. POST is non-idempotent which means multiple requests will have different effects.
3.PUT method requests the server to update a resource or create the resource if it doesn’t exist. E.g. PUT /movie/23 will request the server to update, or create, if doesn’t exist, the movie with id =23. PUT is idempotent, which means multiple requests will have the same effects.
4.DELETE method requests that the resources, or their instance, should be removed from the database. E.g. DELETE /actors/103 will request the server to delete an actor with ID=103 from the actor’s collection.
HTTP response status codes?
HTTP status codes are a set of standardized codes which has various explanations in various scenarios
Successful responses
200 OK 201 Created 202 Accepted
Client error responses
400 Bad Request 404 Not Found 405 Method Not Allowed
Server error responses
500 Internal Server Error 501 Not Implemented 503 Service Unavailable
List of movies-array of reference
movies: [{
type: mongoose.Schema.ObjectId,
ref: ‘Movie’
}]
By doing so, each movie document can reference a set of Actors.
getAll
getAll: is a function that retrieves all the documents from the Actor collection and sends them back as a response.
getAll: function (req, res) {
Actor.find(function (err, actors) {
if (err) {
return res.json(err);
} else {
res.json(actors);
}
});
},
createOne:
: is a function that creates a new document based on the parsed data in ‘req.body’ and saves it in the ‘Actor’ collection.
createOne: function (req, res) {
let newActorDetails = req.body;
newActorDetails._id = new mongoose.Types.ObjectId();
let actor = new Actor(newActorDetails); actor.save(function (err) { console.log('Done'); res.json(actor); }); },
OR
createOne: function (req, res) {
let newActorDetails = req.body;
newActorDetails._id = new mongoose.Types.ObjectId();
Actor.create(newActorDetails, function (err, actor) { if (err) return res.json(err); res.json(actor); }); },
getOne
getOne: function (req, res) {
Actor.findOne({ _id: req.params.id })
.populate(‘movies’)
.exec(function (err, actor) {
if (err) return res.json(err);
if (!actor) return res.json();
res.json(actor);
});
},
getOne: this function finds one document by an ID
.populate replaces each ID in the array ‘movies’ with its document. For example, if there is an actor with two movies, then the output of the above function will be:
{
“movies”: [
{
“actors”: [],
“_id”: “5b89971ce7ef9220bcada5c2”,
“title”: “FIT2095”,
“year”: 2015,
“__v”: 0
},
{
“actors”: [],
“_id”: “5b8997c4e7ef9220bcada5c3”,
“title”: “FIT1051”,
“year”: 2020,
“__v”: 0
}
],
“_id”: “5b89692872b3510c78aa14f2”,
“name”: “Tim”,
“bYear”: 2013,
“__v”: 2
}
UpdateOne: this function finds a document by its ID and sets new content that is retrieved from ‘req.body’
updateOne: function (req, res) {
Actor.findOneAndUpdate({ _id: req.params.id }, req.body, function (err, actor) {
if (err) return res.status(400).json(err);
if (!actor) return res.status(404).json();
res.json(actor); }); },
deleteOne: this function deletes the document that matches the criteria.
deleteOne: function (req, res) {
Actor.findOneAndRemove({ _id: req.params.id }, function (err) {
if (err) return res.status(400).json(err);
res.json(); }); },
Testing RESTful app
1.frontend generating HTTP requests
2.RESTful clients that simulate the client side and generate requests based on our design.
consider the following code:
getOne: function (req, res) {
Movie.findOne({ _id: req.params.id })
.populate(‘actors’)
.exec(function (err, movie) {
if (err) return res.status(400).json(err);
if (!movie) return res.status(404).json();
res.json(movie);
});
}
Which of the following is true?
‘actors’ a field represents an ID (reference) or an array of IDs