一个节点可以定义一些属性作为 credentials
(凭证)。这些属性与主流文件分开存储,并且在从编辑器导出流时不会被包含在内。
要将凭证添加到节点,请执行以下步骤:
credentials
条目。 credentials: {
username: {type:"text"},
password: {type:"password"}
},
这些条目只有一个选项——它们的 type
(类型),可以是 text
(文本)或 password
(密码)。
<div class="form-row">
<label for="node-input-username"><i class="fa fa-tag"></i> Username</label>
<input type="text" id="node-input-username">
</div>
<div class="form-row">
<label for="node-input-password"><i class="fa fa-tag"></i> Password</label>
<input type="password" id="node-input-password">
</div>
请注意,模板使用的元素 id
约定与常规节点属性相同。
.js
文件中,必须更新对 RED.nodes.registerType
的调用以包含凭证。 RED.nodes.registerType("my-node",MyNode,{
credentials: {
username: {type:"text"},
password: {type:"password"}
}
});
在运行时,节点可以使用 credentials
属性访问其凭证。
function MyNode(config) {
RED.nodes.createNode(this,config);
var username = this.credentials.username;
var password = this.credentials.password;
}
在编辑器中,节点对其凭证的访问受限。任何类型为 text
的凭证都可以在 credentials
属性下访问——就像在运行时一样。但类型为 password
的凭证不可用。取而代之的是,会有一个名为 has_<属性名>
的相应布尔属性,用于指示该凭证是否已分配了非空值。
oneditprepare: function() {
// this.credentials.username is set to the appropriate value
// this.credentials.password is not set
// this.credentials.has_password indicates if the property is present in the runtime
...
}
尽管上述凭证系统在大多数情况下已经足够,但在某些情况下,有必要在凭证中存储比用户提供的值更多的信息。
例如,一个节点要支持 OAuth 工作流,它必须保留用户永远不会看到的由服务器分配的令牌。Twitter 节点提供了一个如何实现这一目标的好例子。
版权所有 OpenJS Foundation 和 Node-RED 贡献者。保留所有权利。OpenJS Foundation 拥有注册商标并使用商标。有关 OpenJS Foundation 的商标列表,请参阅我们的商标政策和商标列表。未在 OpenJS Foundation 商标列表中注明的商标和徽标是其各自所有者的商标™或注册®商标。使用它们并不意味着与它们有任何关联或得到它们的认可。
The OpenJS Foundation | 使用条款 | 隐私政策 | OpenJS Foundation 章程 | 商标政策 | 商标列表 | Cookie 政策