存储 API

存储 API 提供了一种可插拔的方式来配置 Node-RED 运行时存储数据的位置。

API 存储的信息包括

  • 流配置
  • 流凭据
  • 用户设置
  • 用户会话
  • 节点库内容

默认情况下,Node-RED 使用此 API 的本地文件系统实现。

API 函数文档请参见此处

配置

settings.js 中的 storageModule 属性可用于指定要使用的自定义模块

storageModule: require("my-node-red-storage-plugin")

Promise

此 API 广泛使用 JavaScript Promise

Promise 代表异步操作的最终结果。它充当占位符,直到结果可用为止。

Node-RED 使用 When.js 库。以下示例展示了它的用法。有关更完整的示例,默认文件系统实现位于 red/runtime/storage/localfilesystem.js 中。

function getFlows() {
    // create and return a promise
    return when.promise(function(resolve,reject) {
        // resolve - a function to be called with the successful result
        // reject - a function to be called if an error occurs

        // do some asynchronous work, with a callback on completion
        doAsyncWork(function(err,result) {
            if (err) {
                reject(err);
            } else {
                resolve(result);
            }
        });
    });
}

getFlows()
    .then(function(result) {
        // Called when getFlows completes successfully
    })
    .otherwise(function(err) {
        // Called when getFlows hits an error
    });