Skip to content
Taritsyn edited this page Mar 7, 2024 · 16 revisions

JavaScriptEngineSwitcher.Node contains a NodeJsEngine adapter (wrapper for the Jering.Javascript.NodeJS version 7.0.0).

This package does not contain the node.exe. Therefore, you need to install the Node.js and add the node.exe's directory to the Path environment variable (automatically done by the official installer).

Engine settings

You can specify a settings of JS engine during its registration:

engineSwitcher.EngineFactories
    .AddNode(new NodeSettings
    {
        TimeoutInterval = TimeSpan.FromMilliseconds(600),
        UseBuiltinLibrary = true
    })
    ;

If you manually create an instance of JS engine, then you can pass settings via the constructor:

IJsEngine engine = new NodeJsEngine(
    new NodeSettings
    {
        TimeoutInterval = TimeSpan.FromMilliseconds(600),
        UseBuiltinLibrary = true
    }
);

Consider in detail properties of the NodeSettings class:

Property name Data type Default value Description
TimeoutInterval TimeSpan TimeSpan.Zero

Interval to wait before the script execution times out.

UseBuiltinLibrary Boolean false

Flag for whether to use the Node.js built-in library (e.g. require function, process object, etc.).

Runtime settings

In the Jering.Javascript.NodeJS library, the Node.js runtime is represented as a service that implements the INodeJSService interface. The default implementation of INodeJSService is HttpNodeJSService, that can be configured by using the following options:

  1. NodeJSProcessOptions
  2. OutOfProcessNodeJSServiceOptions

Since the Node JS service is a singleton, we cannot create its instance inside the NodeJsEngine class, but we can pass its instance through the constructor:

IJsEngine engine = new NodeJsEngine(new MyNodeJSService());

In the case, if using a version of constructor that does not accept the Node JS service as a parameter, then used the default implementation of service - instance of the DefaultNodeJsService class. DefaultNodeJsService class is a wrapper around the StaticNodeJSService class. Therefore, we can use the Configure method of StaticNodeJSService class to configure the default Node JS service:

using Jering.Javascript.NodeJS;using JavaScriptEngineSwitcher.Core;
using JavaScriptEngineSwitcher.Node;namespace JavaScriptEngineSwitcher.Sample.ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            StaticNodeJSService.Configure<OutOfProcessNodeJSServiceOptions>(
                options => options.ConnectionTimeoutMS = 300);

            using (IJsEngine engine = new NodeJsEngine())
            {}
        }
    }
}

It is worth noting that configuration must be performed before the first instance of NodeJsEngine class is created.

Next, consider examples of the Node JS service configuration in ASP.NET applications.

ASP.NET 4.X

Before reading this subsection, you need to read the information from the “Registration of JS engines > ASP.NET 4.X” subsection.

In ASP.NET MVC, Web Forms and Web Pages configuring of the default Node JS service should be done by using the Configure method of StaticNodeJSService class:

StaticNodeJSService.Configure<OutOfProcessNodeJSServiceOptions>(
    options => options.ConnectionTimeoutMS = 300);

IJsEngineSwitcher engineSwitcher = JsEngineSwitcher.Current;
engineSwitcher.EngineFactories.AddNode();
engineSwitcher.DefaultEngineName = NodeJsEngine.EngineName;

To use your own implementation of the Node JS service you need to pass its instance to one version of the AddNode method:

JsEngineSwitcher.Current.EngineFactories.AddNode(new MyNodeJSService());

ASP.NET Core

Before reading this subsection, you need to read the information from the “Registration of JS engines > ASP.NET Core” subsection.

In ASP.NET Core applications configuring of the default Node JS service should be done by using the dependency injection based API:

services.AddNodeJS();
services.Configure<OutOfProcessNodeJSServiceOptions>(
    options => options.ConnectionTimeoutMS = 300);

services.AddJsEngineSwitcher(options =>
    options.DefaultEngineName = NodeJsEngine.EngineName
)
    .AddNode(services)
    ;

The main feature of this example is that a collection of services is passed to the AddNode method. This is necessary to create an instance of the service with all specified settings.

Known issues

Since this module is not based on JS engine, but on runtime, then it does not fully comply with requirements of the JavaScript Engine Switcher:

  1. Not supported undefined type.
  2. There is no full support of null.
  3. Non-generic versions of the Evaluate, CallFunction and GetVariableValue methods are not supported.
  4. EmbedHostObject and EmbedHostType methods, that responsible for interop, is also not supported.
  5. In the multi-process mode of Jering.Javascript.NodeJS library can not to store a state of JS engine.