nyhunter77 nyhunter77 - 12 days ago 6
Javascript Question

In Node.js, asking for a value using Prompt, and using that value in a main js file

I'm pretty new to and it seems fairly easy to use but when it comes to getting a value using the command line and returning that value to be used in another package or

.js
, it seems harder than I expected.

Long story short, I've used a package (
akamai-ccu-purge
), to enter a file to purge on the network successfully.

I want to make it more dynamic though by prompting the user to enter the file they want purged and then using that in the package.

After making a few tries using
var stdin = process.openStdin();
I actually found another npm package called
Prompt
that seemed to be easier. Both ways seem to have the same problem though.

Node doesn't seem to want to stop for the input. It seems to want to automatically make the purge without waiting for input even though I've called that module first. It actually gets to the point where I should enter the file but it doesn't wait.

I am definitely missing something in my understanding or usage here, what am I doing wrong?

My code so far is:

var purgeUrl = require('./getUrl2');

var PurgerFactory = require('../../node_modules/akamai-ccu-purge/index'); // this is the directory where the index.js of the node module was installed

// area where I placed the authentication tokens
var config = {
clientToken: //my tokens and secrets akamai requires
};

// area where urls are placed. More than one can be listed with comma separated values
var objects = [
purgeUrl // I am trying to pull this from the getUrl2 module
];

// Go for it!
var Purger = PurgerFactory.create(config);

Purger.purgeObjects(objects, function(err, res) {
console.log('------------------------');
console.log('Purge Result:', res.body);
console.log('------------------------');
Purger.checkPurgeStatus(res.body.progressUri, function(err, res) {
console.log('Purge Status', res.body);
console.log('------------------------');
Purger.checkQueueLength(function(err, res) {
console.log('Queue Length', res.body);
console.log('------------------------');
});
});
});


The
getUrl2
module looks like this:

var prompt = require('../../node_modules/prompt');
//
// Start the prompt
//
prompt.start();

//
// Get property from the user
//
prompt.get(['newUrl'], function (err, result) {
//
// Log the results.
//
console.log('Command-line input received:');
console.log(' http://example.com/custom/' + result.newUrl);
var purgeUrl = 'http://example.com/custom/' + result.newUrl;
console.log(purgeUrl);
module.exports = purgeUrl;
});


Thanks again for the help!

Answer

I would probably just allow getURL2 to expose a method that will be invoked in the main module. For example:

// getURL2

var prompt = require('../../node_modules/prompt'); 

module.exports = {
  start: function(callback) {

    prompt.start();

    prompt.get(['newUrl'], function (err, result) {
      // the callback is defined in your main module
      return callback('http://example.com/custom/' + result.newUrl);
    });

  }
}

Then in your main module:

require('./getUrl2').start(function(purgeURL) {
  // do stuff with the purgeURL defined in the other module
});

The implementation may differ, but conceptually, you need to make your second module, which requires some sort of input from the user, happen as a result of that input. Callbacks are a common way to do this (as are Promises). However, as prompt is not necessarily exposing a method that would necessitate a Promise, you can do it with plain old callbacks.

Comments