0

I am new to nodejs, not getting the concept of determining the number of arguments and the name of arguments. See this link. It tells that

    The callback function takes two arguments, request and response. The  
 request object contains information regarding the client’s request, such  
 as the URL, HTTP headers, and much more. Similarly, the response object  
 is used to return data back to the client.

How do we know that
1) It accepts 2 arguments - do we need to check the documentation of nodejs?
2)How do we know the properties of the arguments - first argument is the request and second is response?
3)There is no error argument

1
  • 1
    It's always better to look for definitions in the official documentation (http), especially when compared to a "random" blog post. Commented Jul 1, 2015 at 6:09

1 Answer 1

2

The callback pattern typically takes an argument error, which is the first argument (by convention).

You are, however, talking about express route callbacks, which can take up to three arguments: req, res and next.

As to your questions:

  1. Yes, documentation is always a good thing to check when you are working with APIs. However, it's not nodejs you need to check, but the library you are intending to use (such as Express)

  2. For Express, yes, callback arguments typically take the form of req, res, next. You'll need to check the documentation to see what those objects typically look like, but as a dynamic language, those objects can be anything (in theory).

  3. There is no error argument because the system cannot know what an error is, once control has been given to your function. Did you trigger an http 500 error, or an access denied, or a bad request (http400), and so on. Only your code logic can identify an error (and what type it is) and send the proper response, for example, via a res.status(409) for a conflicting object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.