NetNoder allows you to start/stop and monitor a node.js express server from a .NET application.
Install-Package netnoder
npm install netnoder
var nodeServer = new NodeServer(new NodeServerSettings
{
WindowStyle = ProcessWindowStyle.Hidden,
EntryPointFilePath = @"C:\code\personal\github\netnoder\TestNodeServer\app.js",
KillPassword = "MyPasswordToStopServer",
Location = new NodeServerLocation
{
Host = "localhost",
Port = 9514,
Protocol = "http"
}
});
var success = nodeServer.Start();
if (success)
{
Console.WriteLine("Node server started just fine at " + nodeServer.Address);
Console.WriteLine("Stopping server in 5 seconds");
Thread.Sleep(5000);
nodeServer.Stop();
}
else
{
Console.WriteLine("Could not start node server");
}
You can set up the server to be monitored so that if the server exits for any reason, it will be restarted automatically. Here is an example:
var nodeServer = new NodeServer(new NodeServerSettings
{
WindowStyle = ProcessWindowStyle.Hidden,
EntryPointFilePath = @"C:\code\personal\github\netnoder\TestNodeServer\app.js",
KillPassword = "MyPasswordToStopServer",
MonitorInterval = 5000,
Location = new NodeServerLocation
{
Host = "localhost",
Port = 9514,
Protocol = "http"
}
});
var success = nodeServer.Start();
if (success)
{
Console.WriteLine("Node server started just fine at " + nodeServer.Address);
Console.WriteLine("Monitoring Server Now");
nodeServer.Monitor();
}
else
{
Console.WriteLine("Could not start node server");
}
All options are available in both .NET and node.js.
- Data (Dictionary[string, object]) (optional): A dictionary that can be used to pass any data to the node.js application.
- EntryPointFilePath (string) (required): JavaScript file that needs to be run.
- KillPassword (string) (optional): Password sent to the node.js server in order to validate the kill action. This is to prevent anyone from killing the server.l
- Location (NodeServerLocation) (required): this object allows you to specify host, port and protocol of the node.js server. Default:
null
- ExpectedServerStartupTime (int) (optional): Amount of time that it should take the node.js server to wind up. If the server takes longer then the time specified, the
Start
method will returnfalse
. Default:1000
- WindowStyle (ProcessWindowStyle) (optional): How to display the node.js shell that is started. Default:
ProcessWindowStyle.Normal
; - MonitorInterval (int) (optional): Set interval for the .NET app to monitor the node.js server in order to ensure that it is running. Default:
10000
;
- Address (string): provides a string with the full URI of the node.js server (i.e.: "http://localhost:3000")
- Monitoring (bool):
true
if the server is currently being monitored by the .NET app. - Settings (NodeServerSettings): the options that were provided in the constructor.
All methods are synchronous.
- bool Start() - Attempts to start the node.js server. Returns
true
on success orfalse
on failure. - bool Stop() - Attempts to stop the node.js server. Returns
true
on success orfalse
on failure. - void Monitor() - Checks to see if the server is up on an interval that can be defined via the
MonitorInterval
option. If the server goes down, Monitor will attempt to bring it back up by executingStart()
again. - void StopMonitoring - Stops monitoring the node.js server.
var express = require('express');
var app = express();
var netnoder = require('netnoder')(app);
netnoder.listen(function(){
console.log('Netnoder server listening on port ' + netnoder.location.host + ':' + netnoder.location.port);
});
app.listen(netnoder.location.port, netnoder.location.host, function(){
console.log('Netnoder server listening on port ' + netnoder.location.host + ':' + netnoder.location.port);
});
console.log(netnoder.location.host) ==> "localhost"
- listen([backlog], [callback]) - Starts listening on the host and port specified on the .NET side. This method call the express listen method so you can still pass all the arguments that you can pass to express; just remember to skip the host and port because those are passed automatically.
If you would like to contribute, you may do so to the development branch.