Diego Rios
Diego Rios

Reputation: 505

How do I know which parameters I should use in NodeJS

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.

Upvotes: 0

Views: 62

Answers (3)

Diego Rios
Diego Rios

Reputation: 505

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.

Upvotes: 0

Albert
Albert

Reputation: 94

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

Upvotes: 0

quanfoo
quanfoo

Reputation: 365

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.

Upvotes: 1

Related Questions