Code
Write a few lines of JavaScript that run on each message.
On this page
The Code node runs your JavaScript on every message it receives, on the server. Download it from the Common group of the catalog.
| Setting | What it does |
|---|---|
| Name | A name for your own reference. |
| Number of outputs | How many outputs the node has: Output #1, Output #2 and so on. |
| Code | Your JavaScript. |
What you can use
| Name | What it is |
|---|---|
value | The data of the message. |
send(data) | Sends data to the first output. |
$.send('output2', data) | Sends data to the second output. Use output3, output4 and so on for the next ones. |
instance.replace('{KEY}') | Returns the text with each {KEY} replaced by the variable of that name. |
console.log(...) | Writes a line to the Console or to the terminal. |
throw new Error('...') | Stops the message and reports an error. |
A message that you do not send on does not go further.
Examples
Answer with a greeting:
send({ message: 'Hello, ' + (value.query.name || 'world') + '!' });Send to one of two outputs:
if (!value.product) {
$.send('output2', { error: 'The product is required.' });
return;
}
send({ response: { success: true, product: value.product } });More examples are in the recipes.