Launch a Node.js server from a C # application

There was a requirement that I need to start the Node.js server from a C # application, it is as simple as running a server.jsscript in the Node.js console. However, I'm not quite sure how to achieve this.

Here is what I have learned so far:

There is a file called Node.js in the installation C:\Program Files (x86)\nodejs\nodevars.bat; this is a command-line window for Node.js. To start the server, I could follow these steps:

  • Execute the file nodevars.bat.
  • SendKeys into a new process console window to start the server.

This approach seems a bit fragile. There is no guarantee that the target user will have their Node.js installation in the same place, also sending keys to the process may not be the ideal solution.

Another way could be:

  • Write a batch file that executes nodevars.bat.
  • Run the batch file from a C # application.

This seems like the best approach, however the only problem is what nodevars.batopens in a new console window.

So, to the question (s), is there a way to start the Node.js script server using the functionality built into the Node.js installation? Perhaps sending arguments to node.exe?

+4
source share
2 answers

If you serve several users, that is, as a server, you can use the os-service package and install the Windows service. Then you can start and stop the service using the standard API.

" ", , os-service - . ( , ).

, #, , , :

ProcessStartInfo psi = new ProcessStartInfo();
psi.UseShellExecute = false;   // This is important
psi.CreateNoWindow = true;     // This is what hides the command window.
psi.FileName = @"c:\Path\to\your\batchfile.cmd";
psi.Arguments = @"-any -arguments -go Here";   // Probably you will pass the port number here
using(var process = Process.Start(psi)){
    // Do something with process if you want.
}
+4
+1

Source: https://habr.com/ru/post/1623057/


All Articles