The Scoop - What is "request"?

Whew! They really throw you into the deep end on this one.

I’ve been pouring over pre-written code in The Scoop trying to make sense of it all and am currently stuck with a question I can’t find the answer to. What is “request”? It pops up as an argument (along with URL) in every function in the server.js file I am working in. Here is an example of how and where it appears:

function createArticle(url, request) {
  const requestArticle = request.body && request.body.article;
  const response = {};

  if (requestArticle && requestArticle.title && requestArticle.url &&
      requestArticle.username && database.users[requestArticle.username]) {
    const article = {
      id: database.nextArticleId++,
      title: requestArticle.title,
      url: requestArticle.url,
      username: requestArticle.username,
      commentIds: [],
      upvotedBy: [],
      downvotedBy: []
    };

    database.articles[article.id] = article;
    database.users[article.username].articleIds.push(article.id);

    response.body = {article: article};
    response.status = 201;
  } else {
    response.status = 400;
  }

  return response;
}

My obvious guess is URL represents the URL of the article, but request? I tried finding an example of the createArticle()function being called in other .js files included in the project, but I can’t find anything that would indicate what kind of thing “request” represents. Any help would be greatly appreciated! Thankyou!

The request parameter represents an HTTP request. Given that the function name is prefixed with create and the first line of the function accesses request.body, this would appear to be handling HTTP POSTs that create new articles.

Also, although I don’t know what the rest of the code looks like, or what libraries are used, I would imagine that you will not find any direct invocations of this function. However, you will likely find some sort of “route table” where this function is registered as a “handler” for HTTP POST requests for some URL pattern, such as /articles.

1 Like

You’re spot on. This function is a POST method for an ‘articles’ route. Some of these projects are really advanced compared to the content of the lesson that is attached to (probably on purpose!), so I’ve had trouble breaking down the code and understanding what the syntax is doing. Is there a keyword you could recommend on google that would help me learn more? I can’t find any info on request.body anywhere. Is this a pattern you have simply noticed by doing this so many times?

Thank you!

Another question. When you see the prefix create is it safe to assume that the request is the content of the thing that’s being created? Like in some weird world where you say createFruit, you would assume the request would be an apple or a pear… etc.

For example a portion of the function below in the same .js file:

function getOrCreateUser(url, request) {
  
  const username = request.body && request.body.username;
  const response = {};

  if (database.users[username]) {
    response.body = {user: database.users[username]};
    response.status = 200;
  } else if (username) {
    const user = {
      username: username,
      articleIds: [],
      commentIds: []
    };
    database.users[username] = user;

    response.body = {user: user};
    response.status = 201;
  } else {
    response.status = 400;
  }

  return response;
};

At the very beginning, am I right in thinking that the information being fed into the request argument is the same information that is being typed into the username login field? If so, is const username = request.body && request.body.username saying that we are creating a variable username from the content (body) of the request (in this case the username that was typed in) AND THEN (&&) we are sort of on-the-fly redefining the username variable to be a property where username: "(whatever username i typed into the field)"

Am I even remotely close to thinking about this correctly? Thank you so much!!

This is simply about making HTTP requests, but since I don’t know which library or built-in API your example is using to make those requests, I can’t give you anything more specific to search for than “javascript http requests”.

There are many ways to make HTTP requests, so you’ll see various specific coding patterns, but they all follow a similar general pattern of defining “handlers” for the various HTTP methods (GET, POST, etc.) in which the handlers deal with request and response objects. Because I’ve worked with this topic before, it’s easy to spot the pattern.

You have the right idea, yes, but we are not “redefining” anything. More accurately, we are simply defining a new variable username and setting its value to the value of the username property contained within the body object property of the request object.

The conditional logic that follows that first checks to see if the specified user is already in our database. If so, return the existing user with a response code of 200 (success), otherwise, if a username was actually supplied in the response body object, then create the user and put it in our database and return a response with a response code of 201 (created), otherwise, return a response with a status code of 400 (bad request – meaning that the information supplied in the request was not valid, which in this case means that no username was supplied).

Excellent - I have discovered a few clues through searching through the dependencies information in package.json. It looks like whatwg-fetch could be the library for requests that it’s using. I’ve started researching the documentation for fetch so hopefully that will help me understand what’s going on a bit better. Thank you!

Gotcha - can I ask a dumb question? Why couldn’t we just write

const username = request.body.username?

What is the request.body && doing for us? I’m only familiar with && in the context of being a conditional in an if/then statement. Does it mean something else in this case?

That’s not a dumb question. The request.body && in front is making sure that the request object has a value for the body property before we attempt to get the username property from the body.

If request does not have a body property, then the expression request.body.username will result in an error: TypeError: Cannot read property 'username' of undefined. Therefore, placing request.body && in front acts as a guard against such an error, and instead will just cause the variable username to be assigned undefined.

That makes sense - thank you for all the replies!

This lesson is nuts. It doesn’t at all prepare you to do this lesson. I’m so lost.

I agree - the lessons with no video solution tend to be the hardest I’ve found. But I believe the key to understanding this lesson is in the code that already exists, along with some of the tips in this thread, and googling anything that doesn’t look familiar. I’ve been slowly fiddling with it and am at least making small bits of progress.

Stick with it - In the prior two cumulative projects (Beat Mix and Rock Paper Scissors) I went to the solution code way too quickly. I feel like even though I’ve only figured out 10% of this project so far, I’m learning more than I did by giving up early.

@designwhiz48643 I can confirm the key to solving this project is indeed in the analysis and reproduction of the code already provided, along with googling anything in that code you don’t understand. Keep at it.

This is my first cumulative project I was able to complete without going to the code solution. Thank you @chuckwondo and @mtf!

1 Like