如何防止nodejs控制台关闭

时间:2016-01-16 11:42:25

标签: node.js

nodejs中有PHP error_reporting(0)的替代方案吗?我想阻止应用在发生错误后关闭,我尝试了try and catch method,但它不起作用。 那么,如何在发生错误后阻止node.js server关闭?

2 个答案:

答案 0 :(得分:0)

编辑: 有一个未被捕获的错误事件:

unsigned char get_cplc_command2[] = {0x00, 0xA4, 0x00, 0x00, 0x02, 0x02, 0x03};
receive_length = sizeof(receive_buffer);

ret = SCardTransmit(card,
                    &sendPCI,
                    get_cplc_command2,
                    sizeof(get_cplc_command2),
                    NULL,
                    receive_buffer,
                    &receive_length);

LOG(@"SCardTransmit 0x%08x", ret);

if (ret == SCARD_W_REMOVED_CARD)
{
    goto retry;
}
else if (ret != SCARD_S_SUCCESS)
{
    [err_text setString:@"SCardTransmit failed on CPLC (w/ Le > 0)"];
    goto cleanup;
}


// GET RESPONSE
unsigned char get_cplc_command3[] = {0x00, 0xC0, 0x00, 0x00, 0x12};
receive_length = sizeof(receive_buffer);

ret = SCardTransmit(card,
                    &sendPCI,
                    get_cplc_command3,
                    sizeof(get_cplc_command3),
                    NULL,
                    receive_buffer,
                    &receive_length);

LOG(@"SCardTransmit 0x%08x", ret);


if (ret == SCARD_W_REMOVED_CARD)
{
    goto retry;
}
else if (ret != SCARD_S_SUCCESS)
{
    [err_text setString:@"SCardTransmit failed on CPLC (w/ Le > 0)"];
    goto cleanup;
}

NSMutableString* cplc = [[NSMutableString alloc]init];

if (receive_buffer[receive_length-2] == 0x90 && 
    receive_buffer[receive_length-1] == 0x00)
{

    unsigned char* p = receive_buffer + 3;        



    NSUInteger size = sizeof(p);

    NSMutableString *mutString =[[NSMutableString alloc] init];

    for(int i=0;i<=size-1;i++)
    {
        [mutString appendString:[NSString stringWithFormat:@"%02X",p[i]]];
    }

    NSString * str = mutString; //@"8510000002030100043DC000C0000000006A9000";
    NSMutableString *newString = [[[NSMutableString alloc] init] autorelease];
    int i = 0;
    while (i < [str length])
    {
        NSString * hexChar = [str substringWithRange: NSMakeRange(i, 2)];
        int value = 0;
        sscanf([hexChar cStringUsingEncoding:NSASCIIStringEncoding], "%x", &value);
        [newString appendFormat:@"%c", (char)value];
        i+=2;
    }

}

请参阅重复:Make node.js not exit on error

您还可以使用process.on('uncaughtException', function (err) { console.log('Caught exception: ' + err); }); 来运行脚本,pm2会在崩溃时自动重启您的脚本。

您需要捕获错误。 例如

pm2

据我所知,没有其他办法。所以你只能修复或捕获这些错误。

答案 1 :(得分:0)

查看节点的源代码后,发现有一种漂亮的方法可以实现现有的“ ctrl + c”。

创建文件block.js,内容为:

secure

然后在任何主要js文件中都需要此js文件,然后运行此文件:

const readline = require('readline')

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})

const block = (data) => {
    process.on('uncaughtException', function (err) {
        console.log('Caught exception: ' + err)
    })
    rl.on('SIGINT', () => {
        console.log(data)
        rl.pause()
    })
}

module.exports = {
    block
}

block.js文件将捕获err和“ ctrl + c” SIGINT退出。