stdin, stdout and stderr with node.script
The max-api module provides API functionality that allows your JavaScript program to output messages to Max and react to incoming messages from Max. Another typical way of interacting with a program is using the Standard Input, Standard Output and Standard Error streams, which provide access to incoming and outgoing text streams of your program. The typical “Hello World” logging example would look like the following in a NodeJS program:
console.log(“Hello world”);
NodeJS also provides direct access to the streams via:
- process.stdout
- process.stdin
- process.stderr
as documented here .
The node.script object provides access to these in order to interact with your program. In order to send input into the Node JS program you can use the message:
Note that a node.script automatically appends a newline character to the end of the message. To receive and parse the input from the Standard Input stream NodeJS provides a built-in module called readline. Using the readline module one can parse the input line by line and in this example post to the Max Console:
const readline = require("readline")
const Max = require("max-api")
const rl = readline.createInterface({
input: process.stdin,
terminal: false
})
rl.on("line", async line => {
// This will be posted to the Max console
await Max.post(line)
})
In order to react to the stdout and stderr output from a node.script, you can route the output manually, or you can use the node.debug utility , which provides a Console Tab with a logging display. The following example simply routes the stdout and stderr messages directly to the print object and logs the program’s output to the Max Console in that way.
See Also
Name | Description |
---|---|
Node for Max Documentation - Table of Contents | Node for Max Documentation - Table of Contents |
node.script |