简体   繁体   中英

Create commandline tool NodeJs

I need to create a NodeJs tool for quickly connect and manage some API. I have already developed codes for authentication, get users and create new users.

This is my first experience with NodeJs and I couldn't find a quick guide to resolve some issues. One of my issues in this moment, is to make the values parametric, such as username and password for authentication. Now i put thees values inside the code but i need to manage into a main module by commandline and send this data to other modules.

Can you recommend some modules that I can study to integrate these features? For example commander, inquirer or other. I don't know which one to choose to have a correct management of my modules.

Thanks @Sergeon. I think I have solved at least a first problem. With this example code:

var request = require("request");
var myArgs = process.argv.slice(2);

var username = myArgs[0];
var password = myArgs[1];

//var username = "mif4cc17ciul0"
//var password = "Tr0ppR03"

request.post({
        "headers": { "content-type": "application/x-www-form-urlencoded" },
        "url": "https://api.site.com/v1/token",
        "auth": {
            "user": username,
            "pass": password
        },
        "form": {
            "grant_type": "password",
            "username": "user_test",
            "password": "User-00",
            "scope": "6780f180-4ae5-8617-5d8t-f267f5045d4"
        },
    },
    function(err, response, body) {
        var jsonResponse = JSON.parse(body);
        if ("access_token" in jsonResponse) {
            global.mytoken=(jsonResponse.access_token);
            console.dir(mytoken);
        }
    }
);

module.exports = request

I request the token and manage to put at least the first two values (eg: token.js username password) from the command line and does it work. It doesn't in a best way, but it's already a start. Next I will have to define the values with which to pass the variables, through like inquirer module (eg: node token.js -u username -p password ).

Anyway previously, if I'll want to create a main process to call this code. I think it can also help me to better manage inquirer module. So, how can I send the variables?

The final goal should be: node main.js username password or the best node main.js -u username -p password

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