At the MVP Global Summit, Steven Sanderson (@stevensanderson) presented a Microsoft project he was working on: Node Services. In a nutshell, this is an integration of ASP.NET 5 and Node.js, it makes it possible to call a Node.js function from ASP.NET. One of its possible usages is to use Node.js to compile AngularJS directives or ReactJS JSX files, and for that reason, there are two modules built on top of Node Services just for that purpose (code available at GitHub and NuGet, here and here).
Disclaimer: this is still in early stage, and is likely to change!
So, after you install the NuGet package using the now familiar syntax:
You need to register the Node Services it in Startup.cs’s ConfigureServices method:
Now, there are two ways by which ASP.NET can communicate with the Node.js host:
Creating a Node.js module is easy; let’s add a .JS file to the project:
For simplicity’s sake, we are doing a very simple operation: just adding two numbers, but in a real-life scenario you can require any other Node.js modules and do any arbitrarily complex operations. You need to be aware of a couple of things:
And how do you call this? You just need a reference to the registered instance of INodeService, and on it you call a module asynchronously, passing it some parameters:
You can also specify the module and function name explicitly, if you export it from the .JS file (not in this example):