Running tomcat from nodejs

I am trying to use a child process in nodejs to start a tomcat server on a local machine. This is an experiment that will help me understand how the children's process works, and will help in the project I'm working on. I looked at the documentation here: http://nodejs.org/api/child_process.html , but I have a bit of a problem that really does this.

What I'm trying to do is run nodejs somewhere locally, as soon as I click somewhere (or even launch the page), the page should start the tomcat server, make sure it is loaded, and then load the localhost: 8080 welcome page. As soon as I close the page here, the nodejs APIs are called, it should disable tomcat (this part is not needed now, but just part of the experiment). Thanks!

+4
source share
1 answer

Take a look at this npm Shelljs module

ShellJS - Unix Shell Commands for Node.js Build Status

ShellJS is a portable (Windows / Linux / OS X) implementation of Unix shell commands on top of the Node.js API. You can use it to eliminate your Unix-dependent script shell while maintaining your familiar and powerful commands. You can also install it globally so you can run it from outside Node projects - say goodbye to those gnarly Bash scripts!

ShellJS has been tested in many related projects.

Since it can call a script, you can control Tomcat scripts to start and shut down and continue the experiment :)

ShellJS has an option:

exec (command [, options] [, callback])

Available options (all false by default):

async: Asynchronous execution. Defaults to true if a callback is provided. silent: Do not echo program output to console. 

Examples:

var version = exec ('node --version', {silent: true}). output;

var child = exec ('some_long_running_process', {async: true}); child.stdout.on ('data', function (data) {/ * ... do something with the data ... * /});

exec ('some_long_running_process', function (code, output) {
console.log ('Exit code:', code); console.log ('Program output:', output); });

Executes this command synchronously, unless otherwise specified. When the object {code: ..., output: ...} containing the program output (stdout + stderr) and the exit code is returned in synchronous mode. Otherwise, it returns a child of the process, and the callback receives arguments (code, output).

Note. For long-running processes, it is best to run exec () asynchronously since the current synchronous implementation uses a lot of CPU. This should be fixed soon.

+2
source

All Articles