dnsdnsdns
dnsdnsdns

Reputation: 21

Understanding Express documentation for middleware

I am a beginner and struggle to read documentation although, practically, I can understand what the documentation says by looking at the code implementation.

For example, app.use([path,] callback [, callback...])

I understand how to use the method but I still have no clear understanding of the notation used in the documentation. For instance, questions that I have in mind:

  1. Why is the path inside a square bracket?
  2. What does the comma mean after it? Does it mean that I can add more than one path?
  3. After the callback, we have a square bracket containing the comma and callback and triple dots, what does it mean?

I hope somebody can help me, thanks!

Upvotes: 2

Views: 79

Answers (1)

ggorlen
ggorlen

Reputation: 57234

For general context, use adds a middleware function(s) to the Express request handler.

  1. The path parameter is in a square bracket because it's optional. If you omit it, then the middleware callback is applied to all incoming requests. If you provide a path string, then only requests matching the path will be passed to the middleware callback.
  2. No, it just means that when you do use path, there is a comma delimiting it from the required callback second parameter. Because the second parameter callback isn't in brackets, it's required. The parameter list would look weird if this comma wasn't there after path, because it wouldn't match the JS syntax you'd use to provide multiple parameters to a function.
  3. The triple dots in callback... means you can provide multiple callbacks.

Upvotes: 1

Related Questions