Node-RED使用指南:23:嵌入Node.js应用
发布日期:2021-06-30 20:18:18 浏览次数:2 分类:技术文章

本文共 3467 字,大约阅读时间需要 11 分钟。

在这里插入图片描述

Node-RED可以独立运行,也可以直接嵌入到Node.js应用中,这篇文章以具体的示例来进行说明。

环境说明

  • 操作系统
liumiaocn:~ liumiao$ sw_versProductName:	Mac OS XProductVersion:	10.15.2BuildVersion:	19C57liumiaocn:~ liumiao$
  • node版本
liumiaocn:~ liumiao$ node -vv10.15.3liumiaocn:~ liumiao$ npm -v6.4.1liumiaocn:~ liumiao$
  • Node-RED版本
liumiaocn:~ liumiao$ node-red -hNode-RED v1.0.4Usage: node-red [-v] [-?] [--settings settings.js] [--userDir DIR]                [--port PORT] [--title TITLE] [--safe] [flows.json]Options:  -p, --port     PORT  port to listen on  -s, --settings FILE  use specified settings file      --title    TITLE process window title  -u, --userDir  DIR   use specified user directory  -v, --verbose        enable verbose output      --safe           enable safe mode  -?, --help           show this helpDocumentation can be found at http://nodered.orgliumiaocn:~ liumiao$

代码准备

准备如下基于express的示例代码,将本地的node-red嵌入到此应用之中

liumiaocn:nodered liumiao$ lssample.jsliumiaocn:nodered liumiao$ cat sample.js var http = require('http');var express = require("express");var RED = require("node-red");// Create an Express appvar app = express();// Add a simple route for static content served from 'public'app.use("/",express.static("public"));// Create a servervar server = http.createServer(app);// Create the settings object - see default settings.js file for other optionsvar settings = {
httpAdminRoot:"/red", httpNodeRoot: "/api", userDir:"/Users/liumiao/.node-red", functionGlobalContext: {
} // enables global context};// Initialise the runtime with a server and settingsRED.init(server,settings);// Serve the editor UI from /redapp.use(settings.httpAdminRoot,RED.httpAdmin);// Serve the http nodes UI from /apiapp.use(settings.httpNodeRoot,RED.httpNode);server.listen(8000);// Start the runtimeRED.start();liumiaocn:nodered liumiao$

注意事项:/Users/liumiao/.node-red请修改为自己的本地Node-RED的相应目录。

依赖准备

使用如下命令安装所需依赖

执行命令:npm install express node-red

liumiaocn:nodered liumiao$ npm install express node-red...省略+ express@4.17.1+ node-red@1.0.4updated 2 packages in 3.505sliumiaocn:nodered liumiao$

启动服务

使用如下命令启动服务,因为此Node.js服务本身没有添加功能,所以看起来似乎就是Node-RED,但是我们知道这是我们使用node在8000启动的新的服务

liumiaocn:nodered liumiao$ node sample.js 11 Mar 06:21:28 - [info] Welcome to Node-RED===================11 Mar 06:21:28 - [info] Node-RED version: v1.0.411 Mar 06:21:28 - [info] Node.js  version: v10.15.311 Mar 06:21:28 - [info] Darwin 19.2.0 x64 LE11 Mar 06:21:28 - [info] Loading palette nodes11 Mar 06:21:28 - [info] Context store  : 'default' [module=memory]11 Mar 06:21:28 - [info] User directory : /Users/liumiao/.node-red11 Mar 06:21:28 - [warn] Projects disabled : set editorTheme.projects.enabled=true to enable11 Mar 06:21:28 - [info] Flows file     : /Users/liumiao/.node-red/flows_liumiaocn.json11 Mar 06:21:28 - [warn] ---------------------------------------------------------------------Your flow credentials file is encrypted using a system-generated key.If the system-generated key is lost for any reason, your credentialsfile will not be recoverable, you will have to delete it and re-enteryour credentials.You should set your own key using the 'credentialSecret' option inyour settings file. Node-RED will then re-encrypt your credentialsfile using your chosen key the next time you deploy a change.---------------------------------------------------------------------11 Mar 06:21:28 - [info] Starting flows11 Mar 06:21:28 - [info] Started flows

使用/red的URL即可在8000端口确认在此Node.js应用中嵌入的Node-RED功能了

在这里插入图片描述

转载地址:https://liumiaocn.blog.csdn.net/article/details/104788664 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Node-RED使用指南:24:嵌入到Angular应用中
下一篇:Node-RED使用指南:22:定制化Node的使用方法

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年04月11日 04时22分07秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章