"# Node-RED editor-api \u90e8\u5206\u6e90\u7801\u89e3\u6790\n\n> 2023-02-14 | 2023-02-14\n> https:\/\/www.modbus.cn\/en\/11790.html\n\n**Node-RED**\n\n---\n\n## \u80cc\u666f\n\n\u6700\u8fd1\u603b\u6709\u8bfb\u8005\u6765\u5411\u6211\u8be2\u95ee\u5982\u4f55\u5b9a\u5236\u5f00\u53d1Node-RED\uff0c\u4ed6\u4eec\u60f3\u57fa\u4e8e\u81ea\u5df1\u7684\u9879\u76ee\u505a\u4e00\u4e9b\u5b9a\u5236\u5f00\u53d1\u3002\n\u6bd4\u5982\u8c03\u6574Node-RED\u7684\u5e03\u5c40\uff0c\u4e3b\u9898\u8272\uff0c\u589e\u52a0\u4e00\u4e9b\u6309\u94ae\u3002\u6216\u8005\u5c06\u4e00\u4e9b\u5176\u4ed6\u529f\u80fd\u96c6\u6210\u8fdb\u53bb\u3002\n\u63a5\u4e0b\u6765\u7684\u51e0\u7bc7\u6587\u7ae0\u6211\u4eec\u5c31\u6765\u5206\u6790\u4e00\u4e0bNODE-RED\u7684\u6e90\u7801\u3002\n\nNODE-RED\u7684\u6838\u5fc3\u4ee3\u7801\u4e3b\u8981\u5728`packages\/node_modules\/@node-red`&nbsp;\u8be5\u76ee\u5f55\u4e0b\u3002\n\u6839\u636e\u6700\u65b0\u7248,\u8be5\u76ee\u5f55\u4e0b\u67096\u4e2a\u5b50\u76ee\u5f55\uff0c\u5206\u522b\u662f\uff1a\n\n- editor-api \u7f16\u8f91\u5668\u540e\u7aef\u4ee3\u7801\n\n- editor-client \u7f16\u8f91\u5668\u524d\u7aef\u4ee3\u7801\n\n- nodes \u9ed8\u8ba4\u5b89\u88c5\u7684\u8282\u70b9\n\n- registry \u63d2\u4ef6\uff0c\u5e93\uff0c\u8282\u70b9\u7684\u6ce8\u518c\u7ba1\u7406\n\n- runtime \u8fd0\u884c\u65f6\u5165\u53e3\uff0c\u4e5f\u662fnode-red\u7684\u5165\u53e3\n\n- util \u8f85\u52a9\u5de5\u5177\n\n\u672c\u7bc7\u9996\u5148\u8bb2\u89e3\u4e00\u4e0beditor-api\u8be5\u76ee\u5f55\u7684\u4f5c\u7528 \u53ca\u91cd\u8981\u6e90\u7801\u3002\u65b9\u4fbf\u5f00\u53d1\u4eba\u5458\u5f00\u53d1\u65f6\u80fd\u591f\u5feb\u901f\u7406\u89e3\uff0c\u5e76\u627e\u5230\u5bf9\u5e94\u7684\u6587\u4ef6\u3002\n\n## editor-api\n\n\u8be5\u76ee\u5f55\u5b58\u653e\u7684\u662f\u7528\u4e8e\u652f\u6491editor-client\u529f\u80fd\u7684\u540e\u7aefapi\u670d\u52a1\u3002\n\u5b83\u662f\u4f7f\u7528express\u542f\u52a8\u7684\u4e00\u4e2a\u5e94\u7528\u3002\u4f9d\u8d56@node-red\/util\u548c@node-red\/editor-client\u3002\n\u8be5\u6a21\u5757\u7684\u5165\u53e3\u662f`.\/lib\/index.js`\n\n\u5e94\u7528\u521d\u59cb\u5316\u4ee3\u7801\n\n```\nfunction init(settings,_server,storage,runtimeAPI) {\r\n    server = _server;\r\n    if (settings.httpAdminRoot !== false) {\r\n        adminApp = express();\r\n\r\n        var cors = require('cors');\r\n        var corsHandler = cors({\r\n           origin: \"*\",\r\n           methods: \"GET,PUT,POST,DELETE\"\r\n        });\r\n        adminApp.use(corsHandler);\r\n\r\n        if (settings.httpAdminMiddleware) {\r\n            if (typeof settings.httpAdminMiddleware === \"function\" || Array.isArray(settings.httpAdminMiddleware)) {\r\n                adminApp.use(settings.httpAdminMiddleware);\r\n            }\r\n        }\r\n\r\n        var defaultServerSettings = {\r\n            \"x-powered-by\": false\r\n        }\r\n        var serverSettings = Object.assign({},defaultServerSettings,settings.httpServerOptions||{});\r\n        for (var eOption in serverSettings) {\r\n            adminApp.set(eOption, serverSettings&#91;eOption]);\r\n        }\r\n\r\n        auth.init(settings,storage);\r\n\r\n        var maxApiRequestSize = settings.apiMaxLength || '5mb';\r\n        adminApp.use(bodyParser.json({limit:maxApiRequestSize}));\r\n        adminApp.use(bodyParser.urlencoded({limit:maxApiRequestSize,extended:true}));\r\n\r\n        adminApp.get(\"\/auth\/login\",auth.login,apiUtil.errorHandler);\r\n        if (settings.adminAuth) {\r\n            if (settings.adminAuth.type === \"strategy\") {\r\n                auth.genericStrategy(adminApp,settings.adminAuth.strategy);\r\n            } else if (settings.adminAuth.type === \"credentials\") {\r\n                adminApp.use(passport.initialize());\r\n                adminApp.post(\"\/auth\/token\",\r\n                    auth.ensureClientSecret,\r\n                    auth.authenticateClient,\r\n                    auth.getToken,\r\n                    auth.errorHandler\r\n                );\r\n            } else if (settings.adminAuth.tokens) {\r\n                adminApp.use(passport.initialize());\r\n            }\r\n            adminApp.post(\"\/auth\/revoke\",auth.needsPermission(\"\"),auth.revoke,apiUtil.errorHandler);\r\n        }\r\n\r\n        \/\/ Editor\r\n        if (!settings.disableEditor) {\r\n            editor = require(\".\/editor\");\r\n            var editorApp = editor.init(server, settings, runtimeAPI);\r\n            adminApp.use(editorApp);\r\n        }\r\n\r\n        if (settings.httpAdminCors) {\r\n            var corsHandler = cors(settings.httpAdminCors);\r\n            adminApp.use(corsHandler);\r\n        }\r\n\r\n        var adminApiApp = require(\".\/admin\").init(settings, runtimeAPI);\r\n        adminApp.use(adminApiApp);\r\n    } else {\r\n        adminApp = null;\r\n    }\r\n}\n```\n\n\u5728lib\u4e0b\uff0c\u5b58\u5728\u4e09\u4e2a\u76ee\u5f55\uff0c\u5206\u522b\u662fadmin\uff0cauth\uff0ceditor\u3002\n\u4e09\u4e2a\u76ee\u5f55\u4e0b\u90fd\u6709\u4e00\u4e2aindex.js \u6587\u4ef6\uff0c\u8be5\u6587\u4ef6\u5b9a\u4e49\u63a5\u53e3\u8def\u7531\uff0c\u63a5\u53e3\u6240\u9700\u8981\u7684\u6743\u9650\uff0c\u4ee5\u53ca\u5bf9\u5e94\u7684\u5904\u7406\u51fd\u6570\u3002\n\n\u7f16\u8f91\u5668\u7684\u767b\u5f55\u903b\u8f91\uff0c\u9274\u6743\u7b49\u90fd\u662f\u5728\u8be5\u6a21\u5757\u5904\u7406\u3002\n\u767b\u5f55\u9274\u6743\u5728`editor-api\/lib\/auth\/index.js`\n\n\u8be5\u6a21\u5757\u4e0b\u63d0\u4f9b\u7684\u7f16\u8f91\u5668\u76f8\u5173\u63a5\u53e3\u5b58\u653e\u5728`editor-api\/lib\/editor`\u76ee\u5f55\u4e0b\u3002\n\u6bd4\u5982\u56fd\u9645\u5316\uff0c\u8bc1\u4e66\uff0c\u4e3b\u9898\uff0c\u8bbe\u7f6e\uff0c\n\neditor-api\u4e2d\u7684\u63a5\u53e3\u5e95\u5c42\u662f\u8981\u8c03\u7528\u8fd0\u884c\u65f6\u7684\u63a5\u53e3\u3002\u6bd4\u5982admin\/context\u4e2d\u7684\u5220\u9664\u51fd\u6570\u3002\n\u5c31\u662f\u8c03\u7528\u4e86&nbsp;`runtimeAPI`\n\n```\nruntimeAPI.context.delete(opts).then(function(result) {\r\n    res.status(204).end();\r\n}).catch(function(err) {\r\n    apiUtils.rejectHandler(req,res,err);\r\n})\n```\n\n\u770b\u5230\u8fd9\u91cc\u76f8\u4fe1\u5927\u5bb6\u90fd\u5df2\u7ecf\u660e\u767d\uff0c\u7528\u6237\u70b9\u51fb\u7f16\u8f91\u5668\u4e2d\u7684\u67d0\u4e2a\u6309\u94ae\uff0c\u8bf7\u6c42\u63a5\u53e3\uff0c\n\u7528\u6237\u7684\u8bf7\u6c42\u9996\u5148\u5230\u5e95editor-api\u8be5\u6a21\u5757\u4e2d\uff0c\u6709\u4e9b\u903b\u8f91\u662f\u5728\u8be5\u6a21\u5757\u4e0b\u76f4\u63a5\u5904\u7406\uff0c\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002\n\u800c\u6709\u4e9b\u662f\u53c8\u8c03\u7528\u4e86runtime\u4e2d\u7684\u51fd\u6570\uff0c\u7ecf\u8fc7\u8fd0\u884c\u65f6\u7684\u5904\u7406\u3002\u6700\u7ec8\u8fd4\u56de\u7ed9\u7528\u6237\u7684\u5ba2\u6237\u7aef\u3002\n\n\u6bd4\u5982\n\u6253\u5f00node-red\u7684\u7f16\u8f91\u5668\u4f1a\u8bf7\u6c42\u4e00\u4e2aflows\u7684\u63a5\u53e3\uff0c\n`http:\/\/127.0.0.1:1880\/flows?_=1676345536746`\n\n\u5728admin\/index.js\u4e2d\u53ef\u4ee5\u627e\u5230\n`adminApp.get(\"\/flows\",needsPermission(\"flows.read\"),flows.get,apiUtil.errorHandler);`\n\u5982\u4e0b\u56fe\n\u5b9a\u4e49\u8def\u7531\u5165\u53e3\n\n![](https:\/\/image.modbus.cn\/wp-content\/uploads\/2023\/02\/1676353910-image-950x574.png)\n\n\u5b9a\u4e49\u5904\u7406\u51fd\u6570\n\n![](https:\/\/image.modbus.cn\/wp-content\/uploads\/2023\/02\/1676353924-image-950x574.png)\n\n\u8fd9\u5c31\u662f\u8def\u7531`\/flows`\u5bf9\u5e94\u7684\u5904\u7406\u51fd\u6570`flows.get`\u3002\n\n\u6ce8\u5165\u6b64\u7c7b\uff0c\u4f60\u5728\u6d4f\u89c8\u5668\u4e2d\u770b\u5230\u7684\u6bcf\u4e00\u4e2a\u8bf7\u6c42\u90fd\u53ef\u4ee5\u5728editor-api\u4e2d\u627e\u5230\u5bf9\u5e94\u7684\u8def\u7531\u914d\u7f6e\u3002\n\u5f53\u7136\u6709\u4e9b\u662f\u4f7f\u7528\u7684\u901a\u914d\u7b26\u3002\u8d70\u7684\u9759\u6001\u8d44\u6e90\u52a0\u8f7d\u3002\n\n## \u603b\u7ed3\n\n\u8981\u60f3\u6539\u9020NODE-RED \u5c31\u5fc5\u987b\u61c2\u5f97\uff0c\u5982\u4f55\u6839\u636e\u770b\u5230\u7684\u9875\u9762\uff0c\u63a5\u53e3\u6765\u627e\u5230\u5bf9\u5e94\u7684\u4ee3\u7801\u3002\n\u6709\u65f6\u5019\u4ee3\u7801\u53ea\u9700\u8981\u4fee\u6539\u4e00\u884c\uff0c\u4f46\u627e\u5230\u8fd9\u884c\u4ee3\u7801\u7684\u4f4d\u7f6e\u5374\u9700\u8981\u534a\u5929\u3002\n\n---\n*modbus.cn*\n"