Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express + NodeJS Documentation [closed]

Hi Everyone I am newbie to Express + NodeJS. I come from Java and trying to get caught up on Node now so please be gentle if I ask some basic questions (I have spent the last 2 days catching up on this stuff)

My questions are around documentation.

So when I look through some of the examples present on 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 paramaters go into the function?? first theres was req, res and next but then it had req and res.

Second question: where is the documentation for what is available on the request and response objects along with other method that are available as part of the framework?

I looked up the documenation here http://expressjs.com/guide.html#req.header() but I can't imagine this is entire doc because i notice methods being used on examples that are not present on this documentation.

Can someone point me to the most commonly used docs for express + nodeJS development? If you can list all the links here I would be very grateful!

like image 943
Hans Avatar asked Dec 27 '25 16:12

Hans


1 Answers

First question: You need to slow down a minute. Do you have any experience with another language besides Java? Javascript does not have method signatures in the way you know from Java. Here is 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

It's a contrived example, but it shows that you can declare a function which takes 3 arguments, and only use two of them. In fact, it could use arguments directly and accept even more arguments than specified in the function declaration!

function bar(foo) {
    if(arguments.length > 1) {
        var baz = arguments[1];
        return foo + baz;
    }
    return foo;
}

bar(2);
// => 2
bar(2, 4);
// => 6

Anyway, in short, your route handler function(req, res, next) {} will always be called with req, res, and next -- but it just might be the case that you don't need next (because you won't ever pass on control, perhaps), so you omit it (it will still be there, in your arguments object, just not scoped as next). Some feel this is sloppy, others that it's succinct.

Second question: Here is node.js docs, http.ServerRequest, http.ServerResponse. You already know the Express guide.

like image 113
Linus Thiel Avatar answered Dec 30 '25 06:12

Linus Thiel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!