存储API

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

API存储的信息包括:

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

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

API函数文档请参阅此处

配置

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

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

承诺

该API广泛使用了JavaScript promises

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