使用node-ffi时如何防止Node崩溃

How to prevent Node from crashing when using node-ffi

我有一个 node.js 服务器,它使用 Node-ffi 调用 C++ 代码并将其发送到客户端。我面临的问题是客户端可以发送导致 C++ 程序崩溃的用户输入,我该如何防止这种情况发生?

当C++程序崩溃时,服务器端出现如下输出:

npm ERR! code ELIFECYCLE
npm ERR! errno 3221225477
npm ERR! ws_send_json_server@1.0.0 start: `node app.js`
npm ERR! Exit status 3221225477
npm ERR!
npm ERR! Failed at the ws_send_json_server@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:

我想知道是否可以围绕 node-ffi 函数调用编写 try-catch 块或其他内容,例如:

try {
    greenbuild.GB_SetArchOffset(messageJSON.Item.arch_offset);
    greenbuild.GB_SetArchRadius(messageJSON.Item.arch_radius);
    greenbuild.GB_SetBayLength(messageJSON.Item.bay_length);
    greenbuild.GB_SetBayWidth(messageJSON.Item.bay_width);
    greenbuild.GB_SetPeakHeight(messageJSON.Item.peak_height);
    greenbuild.GB_SetWallHeight(messageJSON.Item.wall_height);
    greenbuild.GB_SetColumnSpacing(messageJSON.Item.column_spacing);
    greenbuild.GB_SetNumBayLength(messageJSON.Item.number_bays_length);
    greenbuild.GB_SetNumBayWidth(messageJSON.Item.number_bays_width);
    greenbuild.GB_SetNumPanelHorzLength(messageJSON.Item.horizontal_panels_length);
    greenbuild.GB_SetNumPanelHorzWidth(messageJSON.Item.horizontal_panels_width)
    greenbuild.GB_SetNumPanelVert(messageJSON.Item.vertical_panels);

} catch (ERROR) {
    console.error("error occurred, but the whole server is still running.")
}

其中 "greenbuild" 是 ffi.Library 的 C++ 代码。

谢谢。

答案是在C++端做输入验证,调试写入文本文件作为崩溃输出。