0

I'm trying to learn every aspect of nodeJS and I was following this tutorial I tried to look at express and node's documentation but it doesn't explain what I want to know.

How do you know which parameters does a function pass?

Here's an example:

How do you know this returns a router?

module.exports = function(router) {

// http:localhost:3000/users

router.post('/users', function(req, res){
    var user = new User();
        user.username = req.body.username;
        user.password = req.body.password;
        user.email = req.body.email;
        if (req.body.username == null || req.body.username == '' || req.body.password == null || req.body.password == '' || req.body.email == null || req.body.email == ''){
            res.send('Ensure username, email and password were provided');
        } else {
            user.save(function(err){
                if (err) {
                    res.send(' Username/email already exists ');
                }   else {
                    res.send('User created ')
                }
            });
        }
});
/*console.log(router);
return router;*/
}

PS. I know I'm using router.post but how can I know that. PS2. I think it's not the same question as the one asking about JS.

Diego Rios
  • 463
  • 1
  • 5
  • 18
  • 3
    RTFM. No seriously, in JS there is no typing so you can’t know the object you’re looking at is a rooter without looking at it first. For this you can simply `console.log` the object and you’ll see it’s attributes. – Ulysse BN Feb 08 '17 at 04:22
  • Side-remark: [both `null` and `''` are falseys](http://stackoverflow.com/a/19839953/1022914) in JavaScript -- meaning you can just do `if (!req.body.username || !req.body.password || ...)` – Mikey Feb 08 '17 at 04:26

3 Answers3

1

You should have a look at the Express docs. Also, debugging your app could be a good start.

Edited: As others mention, you can simply use console.log.

quanfoo
  • 365
  • 2
  • 9
  • I am using `console.log()` I just commented it and I looked at the Express docs but I can't find it. I said that in the post... – Diego Rios Feb 08 '17 at 04:26
  • 1
    Don't use console.log for debugging on anything but the most trivial applications. Get used to using node-inspector or some other runtime debugging tool. – Paul Feb 08 '17 at 04:50
0

It's very helpful when using IDE support live code hints. For myself, I use VScode. It's save me a lot of time by showing code hints timely. I no longer need to search document whenever using them. enter image description here

Albert
  • 94
  • 7
0

I think I got it.

This function:

router.post('/users', function(req, res){
var user = new User();
    user.username = req.body.username;
    user.password = req.body.password;
    user.email = req.body.email;
    if (req.body.username == null || req.body.username == '' || req.body.password == null || req.body.password == '' || req.body.email == null || req.body.email == ''){
        res.send('Ensure username, email and password were provided');
    } else {
        user.save(function(err){
            if (err) {
                res.send(' Username/email already exists ');
            }   else {
                res.send('User created ')
            }
        });
    }
});

is a route method (router).

Routing refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST, and so on).

I could've put any name instead of router and it would've still work because it is returning the router method.

Diego Rios
  • 463
  • 1
  • 5
  • 18