Nodejs程序没有终止

时间:2016-11-22 13:25:28

标签: node.js co node-commander

这个程序不会在控制台终止,我必须使用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;
    }
}

2 个答案:

答案 0 :(得分:0)

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

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();
});

答案 1 :(得分:0)

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();
    })