【问题标题】:Nodejs program doesn't terminateNodejs程序不会终止
【发布时间】:2017-04-06 04:46:49
【问题描述】:

此程序不会在控制台中终止,我必须使用 Ctrl-C。文档没有提供任何线索。尝试了各种方法,例如 Return 但无法让它终止,它只是挂在控制台中。控制台中的最后一件事是“现在我们在这里”。

var fs = require('fs');
var path = require('path');
var co = require('co');
var prompt = require('co-prompt');
var program = require('commander');

program
.arguments('<file>')
.option('-i, --Object name <objectName>', 'DP device IP address')
.action(function(file) {
    co(function *() {
        var objectName = yield prompt('Object Name: ');
        console.log('\nObject Name: %s file: %s',objectName, file);
        notmain(file, objectName);
        console.info('now we are here');
    });
})
.parse(process.argv);

function notmain(file, objectName) {
    try {
        var normalPath = path.normalize(__dirname + '/' + file);
        console.info('\nfile path  ' + normalPath);
        certstring = fs.readFileSync(normalPath).toString();
        console.info('\nstring of cert file is  \n' + certstring);
        clientcert = fs.readFileSync(normalPath).toString('base64');
        console.info('\nbase64 string of cert file is  \n' + clientcert);
        var newJson = {};
        newJson.name = objectName;
        newJson.content = clientcert;
        var newfile = {"file": newJson};
        console.info('\nnew json for cert object  ' +     JSON.stringify(newfile));
        console.info('\nclient certificate read from directory  ');
    } catch (err) {
        console.info('file path  ' + normalPath);
        console.info('client certificate file not found');
        return;
    }
}

【问题讨论】:

  • 唯一可以在 process.exit(0) 中工作的东西,但其他答案说这是最后的手段。程序显然在等待什么,我需要告诉它它已经完成了。
  • co 未定义,并且(无论它是什么)看起来像是需要停止的东西。
  • co 是使用生成器和承诺的异步代码包装器。它在顶部定义。
  • 您使用它似乎很奇怪,但随后您使用 readFileSync 并失去了所有异步优势。

标签: node.js co node-commander


【解决方案1】:
co(function *() {
        var objectName = yield prompt('Object Name: ');
        console.log('\n1 Object Name: %s file: %s',objectName, file);
        var normalPath = path.normalize(__dirname + '/' + file);
        console.info('\n2 file path  ' + normalPath);
        fs.readFile(normalPath, function(err, data) {
            if (err) {
                throw err;
            }
            console.info('\n4 string of cert file is  \n' + data);
            if (!(data.includes('BEGIN CERTIFICATE'))) {
                throw 'invalid file type';
            }
            let b64 = data.toString('base64');
            notmain(b64, objectName, results);
        })
        process.stdin.pause();
    })

【讨论】:

    【解决方案2】:

    控制台正在等待更多输入。尝试在“现在我们在这里”行之后添加它。

    process.stdin.pause();
    

    喜欢这个

    co(function *() {
        var objectName = yield prompt('Object Name: ');
        console.log('\nObject Name: %s file: %s',objectName, file);
        notmain(file, objectName);
        console.info('now we are here');
        process.stdin.pause();
    });
    

    【讨论】:

    • 谢谢,效果很好。我也对代码进行了异步和回调。
    猜你喜欢
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 2020-07-22
    • 1970-01-01
    • 2014-07-10
    • 2011-09-19
    • 1970-01-01
    • 2016-04-16
    相关资源
    最近更新 更多