简体   繁体   中英

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.

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 .

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. 在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM