Missing information on project instructions

Hello, I’m having an issue completing projects toward the end of the JavaScript modules. The instructions are vague on purpose, which I get, but I feel like I’m missing vital information. I’ll explain via an example.

I’m currently working on the “Scoop” project in the back-end JS module. The instructions tell you to write some routes and the logic that those routes will call. The functions you’re supposed to write will each be passed a “url” and “request” parameter. Great!

Now I have to write a bunch of logic to handle that request and url, but I don’t know what the request looks like. I assume it’s an object, but I don’t know how’s it’s laid out. Even looking at the solution code doesn’t provide a clear path. The following is from the solution:

function createComment(url, request) {
  const requestComment = request.body && request.body.comment;
  const response = {};

  if (requestComment && requestComment.body &&
      requestComment.articleId && database.articles[requestComment.articleId] &&
      requestComment.username && database.users[requestComment.username]) {
    const comment = {
      id: database.nextCommentId++,
      body: requestComment.body,
      username: requestComment.username,
      articleId: requestComment.articleId,
      upvotedBy: [],
      downvotedBy: []
    };
  }
};

How am I, as the student, supposed to know where ANY of that stuff comes from in relation to the request? In all the lessons, we were told the request would look like {A: a, B: b} etc. I know that I need the ID, the username, and all that stuff, but without knowing what the request object looks like, how am I supposed to access it?

Am I missing something? I can’t figure out where to functions are being called in order to figure out what they’re being passed.

Thanks!

Run it and print the request and/or look for documentation for the request object.

So, that’s my problem. I can’t find any documentation for it. It’s an offline project, so there’s no immediate feedback from Codecademy. I’m using npm run test to test it.

If I add a console.log(request); to the function, how can I then call that function? If you read my post, that’s the problem I’m having, figuring out WHERE these things are getting called so I can call it manually.

Do you know if I can edit test.js and just add a function call at the end?

Where can I find the project?

You’d call it by making a request, so the parts leading up to that would need to be in place

https://www.codecademy.com/paths/create-a-back-end-app-with-javascript/tracks/bapi-javascript-objects-modules-and-browser-compatibility/modules/the-scoop/informationals/the-scoop

This has been a bit of a common theme in these last few projects. I find myself thinking “I know how to write the logic, but… I have no idea what my function is getting passed.”

Looking at the solution code (that way I don’t need to implement anything, but you can poke at it the same way so that’s fair I think)

function createComment(url, request) {
  console.log('here is the request I got:', request)

The route is relevant too, since I’ll be sending a request to it:

  '/comments': {
    'POST': createComment
  },

…run it

$ node server
Server is listening on 4000

so I’m looking to send a POST request to localhost:4000/comments

$ curl --data '' localhost:4000/comments

That crashes the server. With a helpful error message though:

SyntaxError: Unexpected end of JSON input

yeah fine I’ll give it a JSON object

$ curl --data '{}' localhost:4000/comments

this time the call to console.log goes off:

here is the request I got: { body: {} }

You’ve also got this thing to look at

const requestHandler = (request, response) => {

that request object might be a whole lot more interesting. but all requests go through this function so you can figure out from it how it invokes the handlers
(this is also the function that wants JSON in the POST)

there’s also this:

const http = require('http');
const server = http.createServer(requestHandler);

telling you what library is providing requests to the app, and what you’re responding to.
which brings us to… documentation.
https://nodejs.org/api/http.html#http_http_createserver_options_requestlistener

Thank you a ton for the help. I assume my answer is in there somewhere, but this is extremely overwhelming for this point in the curriculum. Even with your road-map I’m having a hard time figuring out where we are.

I’m not sure how the general documentation would be helpful, but I’ll take a look at what you’ve provided and see if I can figure it out.

I do still believe this is quite a jump, especially considering we shouldn’t need the solution to work it out. All I want is a simple “The request object takes this form:”, but oh well!

Thanks!

Depending on how much code the initial project is, you may be able to do the same things there.

I took a very quick peak and it looked like the handlers were already there, and that big requestHandler function too, I didn’t try running it but maybe the very same things apply.

So seeing as you’re writing an api you must know how to invoke it. Somehow. (I have no clue what you’re doing in general)

If I was lost, then I’d be inclined to google for some tutorial with some 20-line minimal server example using http.createServer, see how those parts connect, make sure I can make a request and get a response back from my code, and after that I’d be reasonably well equipped to look for the same things in this project