[存储 | Storage] 文件节点 file



  • 在 Node-RED 的使用过程中,经常会用到存储的功能,比如访问一个URL以后返回的数据,亦或是在流程执行过程中的一些中间数据。

    一般有三种方式进行存储:

    • 缓存,flow.set() flow.get() 或是 global.set() global.get(),生命周期同app的运行时一致
    • 数据库,使用各种多如牛毛的接口节点来完成(需要额外安装到节点面板里,支持MySQL, MongoDB, Redis 等等主流数据库)
    • 文件,最简单最通用的存储方式

    这里我们简单聊下文件节点。

    在左侧 Storage 类目里面默认有三个和文件有关的 node,如下所示
    0_1490345570492_Screen Shot 2017-03-24 at 16.49.43.png
    从上至下依次是

    • 监控文件并输出最新的内容,相当于 linux 命令 tail -F
    • 读取一个文件并输出它的内容
    • 写入内容到一个文件

    具体的使用方法见这几个node自身的说明(选中以后会出现在右侧的 info 标签里)。
    也可以参考 https://goiot.cc/librae/chat 这个聊天应用对聊天记录以及其它一些数据的存储操作。

    重点需要提到的是,在 Go.IoT 平台上,我们给每个 Node-RED 应用都分配了独立的存储空间,挂载上了一块独占权限的数据磁盘,路径在 /data
    所以如果需要读写文件的话,需要把文件名设置为比如 /data/文件名.txt 这样的绝对路径。
    比如,同样在 librae/chat 这个应用中,我们这样来设计文件操作的流程:

    0_1490346121256_Screen Shot 2017-03-24 at 16.32.04.png

    好了,其它的换楼再聊。
    如果有什么使用中遇到的困难,也可以随意回帖,希望能帮到您。



  • 文件上传

    0_1554713303558_daf03eda-8202-466c-869b-1b923a8afe45-image.png

    [
        {
            "id": "631c8235.0da94c",
            "type": "http in",
            "z": "47c12e6e.b915",
            "name": "",
            "url": "/upload",
            "method": "post",
            "upload": true,
            "swaggerDoc": "",
            "x": 170,
            "y": 260,
            "wires": [
                [
                    "ef2d282b.361418",
                    "dfea7f43.00a13"
                ]
            ]
        },
        {
            "id": "3e5c10fb.06b51",
            "type": "http response",
            "z": "47c12e6e.b915",
            "name": "",
            "statusCode": "",
            "headers": {},
            "x": 610,
            "y": 260,
            "wires": []
        },
        {
            "id": "af211fcc.b3b35",
            "type": "file",
            "z": "47c12e6e.b915",
            "name": "",
            "filename": "",
            "appendNewline": true,
            "createDir": false,
            "overwriteFile": "false",
            "x": 470,
            "y": 260,
            "wires": [
                [
                    "3e5c10fb.06b51"
                ]
            ]
        },
        {
            "id": "2bdbc806.5ab9b8",
            "type": "http in",
            "z": "47c12e6e.b915",
            "name": "",
            "url": "/",
            "method": "get",
            "upload": false,
            "swaggerDoc": "",
            "x": 190,
            "y": 320,
            "wires": [
                [
                    "a0c54f05.259ed"
                ]
            ]
        },
        {
            "id": "f877440e.6f8be8",
            "type": "http response",
            "z": "47c12e6e.b915",
            "name": "",
            "statusCode": "",
            "headers": {},
            "x": 490,
            "y": 320,
            "wires": []
        },
        {
            "id": "a0c54f05.259ed",
            "type": "template",
            "z": "47c12e6e.b915",
            "name": "",
            "field": "payload",
            "fieldType": "msg",
            "format": "handlebars",
            "syntax": "plain",
            "template": "<!DOCTYPE html>\n<html>\n<body>\n<form method=\"post\" enctype=\"multipart/form-data\" action=\"/upload\">\n <div>\n   <label for=\"file\">Choose file to upload</label>\n   <input type=\"file\" id=\"file\" name=\"file\" multiple>\n </div>\n <div>\n   <button>Submit</button>\n </div>\n</form>\n</body>\n</html>",
            "output": "str",
            "x": 340,
            "y": 320,
            "wires": [
                [
                    "f877440e.6f8be8"
                ]
            ]
        },
        {
            "id": "ef2d282b.361418",
            "type": "function",
            "z": "47c12e6e.b915",
            "name": "",
            "func": "msg.payload = msg.req.files[0].buffer.toString();\nmsg.filename = '/data/' + msg.req.files[0].originalname;\nreturn msg;",
            "outputs": 1,
            "noerr": 0,
            "x": 340,
            "y": 260,
            "wires": [
                [
                    "af211fcc.b3b35"
                ]
            ]
        },
        {
            "id": "dfea7f43.00a13",
            "type": "debug",
            "z": "47c12e6e.b915",
            "name": "",
            "active": true,
            "tosidebar": true,
            "console": false,
            "tostatus": false,
            "complete": "true",
            "x": 330,
            "y": 200,
            "wires": []
        },
        {
            "id": "b652cdf1.001d3",
            "type": "inject",
            "z": "47c12e6e.b915",
            "name": "test read",
            "topic": "",
            "payload": "",
            "payloadType": "date",
            "repeat": "",
            "crontab": "",
            "once": false,
            "onceDelay": 0.1,
            "x": 180,
            "y": 420,
            "wires": [
                [
                    "6c0e3bde.c196e4"
                ]
            ]
        },
        {
            "id": "6c0e3bde.c196e4",
            "type": "file in",
            "z": "47c12e6e.b915",
            "name": "",
            "filename": "/data/test.c",
            "format": "utf8",
            "chunk": false,
            "sendError": false,
            "x": 350,
            "y": 420,
            "wires": [
                [
                    "e88252a.0b0e0b"
                ]
            ]
        },
        {
            "id": "e88252a.0b0e0b",
            "type": "debug",
            "z": "47c12e6e.b915",
            "name": "",
            "active": true,
            "tosidebar": true,
            "console": false,
            "tostatus": false,
            "complete": "true",
            "x": 490,
            "y": 420,
            "wires": []
        }
    ]
    


  • 0_1555412203057_556b8491-b566-4162-853b-91180b0382b2-image.png
    求问大神,我是将您的json代码拷贝进去,部署的,为什么一直提示找不到路劲呀,麻烦您了,在做毕设的孩子哭兮兮。


Log in to reply
 

Looks like your connection to NodeBB was lost, please wait while we try to reconnect.