8

Logic arrangement is to describe logic in a visual way, which is used to replace the logic description part in general construction scenarios.

A further logical arrangement is front-end and back-end logical mixing, which generally appears on one-stop paas platform. Today, I will introduce a paas tool node-red that fully implements logical orchestration. The content of this week’s intensive reading is its introduction video: How To Create Your First Flow In Node-RED introduces how to use pure logic orchestration to implement a weather query application, as well as deployment and application migration.

Overview

To run Node-RED locally is very simple, as long as the following two commands:

npm install -g --unsafe-perm node-red
node-red

Then you can see this logical layout interface:

We can use these logical nodes to build front-end websites, back-end services, and most of the development work. It is quite abstract to say that, we will introduce the role of each logical node in detail next, let you understand how these logical nodes are planned and designed, and how the logical arrangement controls R&D specifications to improve R&D efficiency.

Node-RED has a total of 42 logical nodes up to now, divided into six categories according to general purpose, function, network, sequence, analysis, and storage.

All nodes may have left and right connection points, the left connection point is the input, the right connection point is the output, and the special node may have multiple inputs or multiple outputs. In fact, the corresponding code is not difficult to understand, that is, the input parameter and the output parameter.

The following describes the function of each node in turn.

Universal

The general node handles general logic, such as manual data input, debugging, error capture, comments, etc.

inject

Enter the node manually. Some inputs can be generated periodically and consumed by the next node.

For example, some fixed values can be periodically generated, such as this object:

return {
  payload: new Date(),
  topic: "abc",
};

Of course, here is configured with the UI form:

After that is consumption, almost any node can consume. For example, when you use the change node to set some environment variables, or use the template node to set the html template, you can get the variables entered here. If in the template, the variable is accessed through {{msg.payload}} , if it is another form, you can even enumerate the selection directly through the drop-down box.

However, this node is often used to set static variables, and more input is from other programs or users, such as http in , which will be discussed later. In fact, through this combination relationship, we can replace the input of any node from the production node to the inject node, so as to achieve some mock effects, and the inject node also supports the configuration of timing automatic triggering:

debug

For debugging, when any output node is connected to the debug input, the output information will be printed on the console to facilitate debugging.

For example, we will inject input connected to debug input, you can see the results printed in the console after the trigger data:

Of course, if you connect the input to debug, the original logic is interrupted. However, any output node can output to other nodes without limitation. You only need to connect the output to the debug and function nodes at the same time:

complete

Monitor certain nodes to trigger completion actions. Through this node, we can capture the actions triggered by any node, access the debug node to print logs, or the function node to process the logic.

You can monitor all nodes, or visually select which nodes to monitor:

catch

Error capture node, output when any or specified node triggers an error, the output format is:

error.message 字符串
错误消息。

error.source.id 字符串
引发错误的节点的ID。

error.source.type 字符串
引发错误的节点的类型。

error.source.name 字符串
引发错误的节点的名称。(如果已设置)

In fact, each node has a fixed output format, these fixed formats limit the flexibility of development, but mastery can greatly improve the development efficiency, because all the same type of node format is the same, this is the benefit of the rule constraint brought by the logical arrangement .

status

Monitor node status changes.

link in

link out can be connected. link in , link out like a portal, used to organize logic orchestration nodes to make them look easy to maintain.

For example, in the following example, after a weather http in , many logical processing nodes are interspersed, including the template http request service that processes requests to query city weather. Although the overall logic is aggregated, it is rather messy:

A better way is classification, which is similar to the modular behavior in code development, exporting weather services, and directly importing any other modules used. This import action is implemented link in link out -> link in is just a spatial position transformation , The transmission value will not change:

In this way, the module looks much clearer. If you want to know the connection relationship of each "portal", just click on one of them to give a prompt, which looks very convenient:

link out

It link in pair with 060b44a3ab8b87 and is used to derive the input value. After docking with link out , the value can be transmitted like a transmission gate, without visually forming a connection line.

comment

Note, with the link series, it can make the logic orchestration UI easier to maintain.

Combining the example of the original video, for the weather service, there is logic for creating environment variables and query logic. The weather query is also divided into querying the current weather, weather for 5 consecutive days, and querying country information. We can talk about each logical grouping on the UI. And use the comment component to mark the comments for easy reading:

Features

Functional nodes are generally used to process business logic, so they include basic if else, js code, template processing and other functional modules.

function

The core js function module, you can use it to do anything:

Will be transmitted to its input msg objects, the code can be modified by msg conducted away after the object through the output node.

Of course, you can also access and modify nodes, processes, and global variables. This is introduced change

switch

The switch corresponding to the code is just more convenient to use, because we can export different nodes according to different cases:

Pay attention to the figure above. Because there are three branches, the export items of the node have also become three. We can make different connections according to different logics:

change

Used to change environment variables. Environment variables are divided into three types: current node, process (canvas), and global (cross-application). In other words, variables can be stored on a certain node, on the entire canvas, or globally across canvases.

The access parameters are msg. , flow. , global. . After setting these parameters, just like global variables, any node can be used anywhere, which is more convenient.

For example, if the application fixes some URL addresses, it http in node, because it may be accessed in the html or other nodes later. Once you modify it, the impact will be very wide, so It is best to set it as a global variable, and access it in the node through variables:

In fact, in the console, you can see the values of these three variables:

After we use the change node to assign values, we can view the values of global variables in different scopes through the debug panel:

range

Interval mapping, which maps the value of one range to another range. In fact, function module, but because it is more commonly used, a special node is encapsulated. In fact, users can also encapsulate nodes by themselves. For details, please refer to official document .

The above figure is easy to understand. For example, normalization in data analysis can be achieved with this node.

template

Generate string or json in template mode.

In fact, it can also be replaced by function in essence, but it is highlighted if it is used to write a template, which is more convenient to maintain.

Built-in mustache template grammar, using variables {{}}

For example, we pass inject inject a variable to template , and by debug printing process is like this:

Among them, inject is configured like this:

We can see the msg.name to a string, and then template access name :

delay

Delayed message, a quick tool, can be placed in the middle of any input and output, for example, in the above example, inject triggered and then print the result, you can configure it like this:

trigger

A message trigger, compared to inject , can set when to re-trigger more flexibly.

As can be seen from the configuration, first inject , and then you can wait, or wait to be reset, or periodically trigger (so it is the inject ), where "send the second message to a separate output" and switch There will be one more output port.

Then there is a reset condition, that is, when the value of payload

It can be seen from this component that, in fact, each node can be function node, but by customizing a node, it can be configured with UI instead of code, which is more convenient to use.

exec

Execute system commands, such as ls etc. This is executed in the background of the system instead of the front end, so it is a very dangerous node.

We can write any command in the configuration:

rbe

Exception report node (Report by Exception), such as blocking when the input changes.

The internet

It is used to create network services, such as http, socket, tcp, udp, etc., because others are not commonly used, only http services are introduced this time.

http in

Create an http service, which can be any interface or web service.

When you set Method to post , connect to http response to create a back-end interface; when set to get request, connect to template write an html template, and connect to http response to create a web service.

Although it is difficult to use react or vue frameworks to create web services in this way, custom nodes still create the possibility for them. Perhaps the front-end modular file can really be defined as nodes in series.

http response

http returns, it can only be http in the output of 060b44a3ab9496, and it is always http in pairs with 060b44a3ab9499.

If only the http in but to no avail http response processed the request, the equivalent of the back-end code, but do not call something like:

res.send("hello word");

To send content to the client.

http request

Unlike http in creates an http service, http request directly sends a network request and imports the return value to the output node.

For the example of obtaining the weather in the video, I used http request initiate a request to obtain weather information:

It is not difficult to see that after sending the request, the function node is used to process the returned result. However, it is still expected to use less function nodes in logical arrangement, because unless there is a good name, it is difficult to see the meaning of nodes. If function processes too much content or function too many 060b44a3ab95c8 blocks, the meaning of logical arrangement will be lost.

sequence

The sequence is the node that processes the array.

split

Corresponding to the code of split , the string becomes an array.

join

The corresponding code join generally split conjunction with 060b44a3ab96ee to facilitate the processing of strings.

sort

Corresponding code sort can only key , which is more convenient for simple scenes, but for complex scenes, function may be used instead.

batch

After receiving the input streams in batches, they are packaged according to the quantity and then output uniformly, which is equal to batch packaging, which can be grouped according to quantity or time interval:

Parsing

It is easy to understand that the data in the above format is specially processed and output according to the data characteristics, such as csv data, which can be output as one message per line, or packaged into a large array and output as one message.

Of course, it can also be replaced by the function node, so the analysis mode and output mode can be customized.

storage

Persistent storage, generally stored as files.

file

The output is a file.

file in

Take the file as input and the result of the file as output.

watch

Monitor changes to directories or files.

intensive reading

After reading the above node-red functions, I believe you already have a more systematic understanding of logical arrangement.

The purpose of logical orchestration is to allow non-R&D people to quickly get started with R&D work. Therefore, it is destined to serve the paas tool. Whether the logical orchestration is useful or not depends on whether the node functions are complete and whether the communication between nodes is smooth. Like the node-red logic orchestration scheme, it is more mature in terms of completeness. It can be said that as long as you have mastered a few core node rules, it is still very efficient to use.

Logical arrangement also has natural disadvantages, that is, when all nodes degenerate to function nodes, there will be two problems:

  • All nodes are function nodes. Even if there are instructions, the internal implementation logic is very free, which makes the logic arrangement unable to restrict input and output.
  • Degrading to code functional calling is essentially the same as writing code. The reason why the logic orchestration improves efficiency is to a large extent that the business logic I want just matches the function of the node, and the efficiency is high when the low-cost UI configuration is implemented.

However, there is also a solution. If your business cannot be satisfied by the existing logic orchestration nodes, you can try to abstract, sort out the commonly used business nodes by yourself, and encapsulate them with a reasonable configuration, as long as the common business logic can be encapsulated as Logical nodes and logical orchestration still have room for business efficiency.

to sum up

Logic orchestration is an extreme, that is, using UI to describe general business logic, reducing the threshold for non-professional developers to get started. Through the analysis of node-red , it can be found that a more complete logic orchestration system can still bring value.

However, for non-professional developers, there is an extreme to reduce costs and improve efficiency, which is completely coded, but the code modularization, function library, tool chain and even low-code platform are so complete that the efficiency of writing code is not low at all. It’s not bad to go to the extreme on this road, because since we need to develop the system in depth, and also invest time in learning, why learning to write code must be less efficient than learning to drag UI? If there are highly encapsulated functions and tool assistance, the efficiency is not necessarily lower than the UI drag and drop.

However, node-red can be further enhanced in the template for creating front-end UI. template from node to UI to build a canvas. The logical arrangement is only used to process logic, so the front-end development experience for large full-stack projects will be better.

The discussion address is: Intensive Reading of "Low Code Logic Arrangement" · Issue #319 · dt-fe/weekly

If you want to participate in the discussion, please click here , there are new topics every week, released on weekends or Mondays. Front-end intensive reading-to help you filter reliable content.

Follow front-end intensive reading WeChat public

Copyright statement: Freely reprinted-non-commercial-non-derivative-keep the signature ( Creative Commons 3.0 License )

黄子毅
7k 声望9.5k 粉丝