w7-RESTful apps Flashcards

1
Q

What is RESTful API?

A

RESTFul API is an application programming interface that uses HTTP requests to perform the four CRUD operations (Create, Retrieve, Update, Delete) on data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

REST?

A

REST - representational state transfer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Is it state or stateless?

A

It is a stateless architecture (sender or receiver retains, i.e. no information) that uses the Web’s existing protocols and technologies.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does REST architecture consist of?

A

The REST architecture consists of:

clients

servers

resources

a vocabulary of HTTP operations known as request methods (such as GET, PUT, or DELETE).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Benefits of REST?

A

-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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

HTTP methods?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

HTTP response status codes?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

List of movies-array of reference

A

movies: [{
type: mongoose.Schema.ObjectId,
ref: ‘Movie’
}]

By doing so, each movie document can reference a set of Actors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

getAll

A

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);
}
});
},

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

createOne:

A

: 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);
}); },
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

getOne

A

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
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

UpdateOne: this function finds a document by its ID and sets new content that is retrieved from ‘req.body’

A

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);
}); },
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

deleteOne: this function deletes the document that matches the criteria.

A

deleteOne: function (req, res) {
Actor.findOneAndRemove({ _id: req.params.id }, function (err) {
if (err) return res.status(400).json(err);

    res.json();
}); },
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Testing RESTful app

A

1.frontend generating HTTP requests
2.RESTful clients that simulate the client side and generate requests based on our design.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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?

A

‘actors’ a field represents an ID (reference) or an array of IDs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

In which scenario would you use PUT http request instead of POST?

A

PUT http requests will be used in a scenario where the client needs to update the resource or create resource if it doesn’t exist. Meanwhile, a POST http request is used more when a client needs to solely request a server to create a resource in database.

17
Q

RESTFul API is a stateless architecture. Elaborate on this statement.

A

sender or receiver saves no information about each other. Session state is not relied on neither client nor server.

18
Q

List two advantages and two disadvantages of using cloud providers to host your project

A

Adv->1.scalability
2.reliability

Disadv->1.security
2.downtime

19
Q

Add a validator to this field such that it only accepts titles with lengths between 5-15 characters.

A

title: {

validate: {

validator: function (newTitle){

if (newTitle.length >= 5 && newTitle.length <= 15)

return true;

else return false

},

message: ‘Title length should be between 5-15 characters.’

},

type: String,

required: true

},