Express + NodeJS Documentation
Hello everyone I am new to Express + NodeJS. I come from Java and am trying to get caught up in Node now, so please be careful if I ask some basic questions (I've spent the last 2 days catching up with this stuff)
My questions are about documentation.
So when I go through some of the examples present on the Express site I see the following
app.get('/users/:id?', function(req, res, next){
......
});
and sometimes
app.get('/users/:id?', function(req, res){
......
});
where is the documentation for me to determine how many parameters are included in a function? at first theres had req, res and next, but then it had req and res.
Second question: where is the documentation for what's available on request and response objects along with another method available as part of the framework?
I have looked at the documentation here http://expressjs.com/guide.html#req.header () but I cannot imagine that this is the whole document because I notice that the methods used are for examples which are not in this documentation ...
Can anyone point me to the most commonly used docs for express + nodeJS? If you can list all the links here, I would be very grateful!
source to share
First question: you need to slow down for a minute. Do you have any experience with a language other than Java? Javascript has no method signatures in the way you know from Java. Here's a quick example:
function foo(bar, baz, qux) {
var result = bar + baz;
if(qux) {
result += qux;
}
return result;
}
foo(1, 2);
// => 3
foo(1, 2, 3);
// => 6
This is a contrived example, but it shows that you can declare a function that takes 3 arguments and only uses two of them. In fact, it could use arguments
directly and take even more arguments than the function's declaration says!
function bar(foo) {
if(arguments.length > 1) {
var baz = arguments[1];
return foo + baz;
}
return foo;
}
bar(2);
// => 2
bar(2, 4);
// => 6
Anyway, your route handler function(req, res, next) {}
will always be called with req
, res
and next
- but it might be that you don't need to next
(because you never get past control, maybe), so you omit it (it will still be there, in your object arguments
just not limited as next
). Some people think it's sloppy, while others feel squeezed.
Second question: node.js docs , http.ServerRequest , http.ServerResponse . You already know the Express manual.
source to share
JavaScript is a bit like the number of parameters a function takes. If the function takes two parameters, you can still pass five arguments - the first two you pass will pass through the named parameters, and the rest will only be accessible through the object arguments
. (Likewise, if you write a function that takes three arguments, the caller can still only pass two, and the third will matter undefined
.)
This way the function will always have three arguments, but many times you have no intention of using a continuation next
, so you can just leave it and JavaScript will silently internalize it.
In terms of documentation, this guide is the official documentation for Express. Docs for Node.js at nodejs.org . Express modules are generally based on their Node vanilla equivalents (for example, the server is based on Node http.Server).
source to share