0

I have a question about running a node.js server. After I have started a server using node server.js, I want to pass a javascript command to the running server. In the same manner that the developer tools console works in a browser, I want to be able to enter a command and have the server execute it. For example, if I have a game server running, and I want to kill a certain player, I could enter into some sort of CLI player.kill(); and the server will execute that command as it is entered. Is this possible? thanks.

eeze
  • 608
  • 1
  • 6
  • 23
  • 2
    So you want to run `node server.js`, have your server listen to `stdin`, and `eval` arbitrary commands in your server's context? – RaphaMex Apr 10 '18 at 00:02
  • 1
    Check out [this](https://stackoverflow.com/questions/32292438/how-can-i-open-a-console-to-interact-with-express-app) and [this](https://stackoverflow.com/questions/14549846/equivalence-of-rails-console-for-node-js) – AbhinavD Apr 10 '18 at 00:13

1 Answers1

0

You can use VorpalJS for this scenario. Vorpal is a framework for building immersive CLI applications built on an interactive prompt provided by inquirer.js.

const vorpal = require('vorpal')();

vorpal
  .command('mycommand1', 'Outputs "bar".')
  .action(function(args, callback) {
    this.log('bar');
    callback();
  });

vorpal
  .delimiter('mygame$')
  .show();
When you execute the above script it creates a new prompt "mygame$" then you can execute the command "mycommand1" in that prompt interactively which can then be passed on to your server.
Community
  • 1
  • 1
tejzpr
  • 945
  • 8
  • 19
  • this is not quite what I want. I need to enter a command to a command line, and have the server execute it as if it were actually a line of code in the server.js – eeze Apr 09 '18 at 23:10
  • @Kristianmitk If you mark a nodejs script as executable in a UnixlLinux OS then the script can be ran without prepending the node executable. The OS will pick up the relevant executable from the [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) – tejzpr Apr 09 '18 at 23:31
  • @Kristianmitk You can asynchronously run commands on the server to dynamically set/retrive data or to do manually triggered backups etc. – tejzpr Apr 09 '18 at 23:40