Back

CreateNode

Hello World
Content:
- Methods

Creates a NodeJS background process with all the functionality of common node.js.

node = app.CreateNode( paths, options )

Note that to define private functions (functions that are invisible by the main app) you have to declare them as global variable without usign the var keyword.
This is actually a safer way of working as it prevents name clashes and libraries overwriting each other's functions and variables accidentally.  Ideally you should put your code into objects or classes for better protection and avoid using many globals.

var myLocalVariable = "Local Hello";
myGlobalVariable = "Global Hello"

function myLocalFunction() { return "Local Hello"; }
myGlobalFunction = function() { return "Global Hello"; }

For more details have a look at the Node docs in the Plugins page

Example - Basic

var nodeJs = 'console.log("Hello World");\nconsole.error("Hello Error");\n'

function OnStart()
{
    app.WriteFile("node_script.js", nodeJs);

    lay = app.CreateLayout("linear", "VCenter, FillXY")
    txt = app.AddText(lay, "Debug Log:", 1, 1, "Log");

    node = app.CreateNode();
    node.SetOnOutput((msg) => { txt.Log(msg); });
    node.SetOnError((msg) => { txt.Log(msg, "red"); });
    node.SetOnMessage((msg) => { txt.Log(msg); });
    node.SetOnReady(node_OnReady);


    app.AddLayout(lay);
}

function node_OnReady()
{
    node.Run("node_script.js");
}
    Copy     Copy All       Run      

Methods

The following methods are available on the Node object:

Execute( js, id )
GetEnv( name ) → String
GetVersion() → Number
IsDone() → Boolean
IsReady() → Boolean
Boolean
List
String
String: javascript code
String: separated
String: path to file or folder ( “/absolute/...” or “relative/...” )
String: path to file or folder ( “/absolute/...” or “relative/...” ): “NODE_PATH env variable”
String: extended.* methods in default main instance”, legacy
String: “name” or “name^ver”
String: optional target directory
String: Context ID
List: argument array
function()
function( msg )
function( stdout )
node.AddModule
Install a node module from npmjs.org
$ node.AddModule(name, dir) $
node.CloseMsgPipe
Close a message pipe
$ node.CloseMsgPipe(isCmd) $
node.Execute
Execute a line of code in the node process.
$ node.Execute(js, id) $
node.GetEnv
Get a process environment variable.
$ node.GetEnv(name) $
node.GetVersion
Returns the plugin version
$ node.GetVersion() $
node.IsDone
Returns if the node process exited
$ node.IsDone() $
node.IsReady
Returns if the Node component is ready for use
$ node.IsReady() $
node.OpenMsgPipe
Open a message pipe
$ node.OpenMsgPipe(isCmd) $
node.Run
Run a NodeJS source file. Use id to run in a new context
$ node.Run(file, id, newPaths) $
node.SendMessage
Send a message to the running node process. Calls cb of parent.SetOnMessage
$ node.SendMessage(msg) $
node.SendPipeMsg
Send a message over the message pipe
$ node.SendPipeMsg(msg, isCmd) $
node.SetEnv
Set a process environment variable.
$ node.SetEnv(name, val) $
node.SetOnDone
Define a callback function which is called when the node process has exited.
$ node.SetOnDone(cb) $
node.SetOnError
Define a callback function which is called when the node process prints to stderr.
$ node.SetOnError(cb) $
node.SetOnMessage
Define a callback function which is called when a system/pipe message was received from the node process.
$ node.SetOnMessage(cb) $
node.SetOnOutput
Define a callback function which is called when the node process prints to stdout.
$ node.SetOnOutput(cb) $
node.SetOnReady
Define a callback function which is called when the Node component is ready for use.
$ node.SetOnReady(cb) $
node.Start
Start the main Node process
$ node.Start(args, paths) $