我对node.js很陌生,它似乎很容易使用,但是当使用命令行获取一个值并返回该值以在另一个包中使用时.js
,它似乎比我预期的要难。
长话短说,我使用了一个npm包 ( akamai-ccu-purge
) 来成功输入要在akamai网络上清除的文件。
我想通过提示用户输入他们想要清除的文件然后在akamai包中使用它来使其更具动态性。
在使用了几次尝试之后,var stdin = process.openStdin();
我实际上发现了另一个名为的npm包Prompt
,它似乎更容易。不过,这两种方式似乎都有同样的问题。
节点似乎不想停止输入。即使我首先调用了该模块,它似乎也想自动进行清除而不等待输入。它实际上到达了我应该输入文件的地步,但它没有等待。
我在这里的理解或使用中肯定遗漏了一些东西,我做错了什么?
到目前为止,我的代码是:
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('------------------------');
});
});
});
该getUrl2
模块如下所示:
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;
});
再次感谢您的帮助!