r/node•
LumpySpoon

Writing commands in cmd while node server is running.

Hello, I've recently been trying to learn how to use node, from a unity c# background. And i have been wondering is it possible to run commands in command prompt, while node server is running? And also with theese commands to invoke functions in the server? Thanks!

Posted Sep 30, 2017, 12:31 PM UTC

15
source-transformer•

The concept you're describing is called interactive mode. Node supports it - if you find it too limited there is also REPL.

2
LumpySpoonOP•

Thank you!

1
MrOCDx3•

Not 100% sure what you are asking. Do you want to have your node app run commands? One option: look at https://www.npmjs.com/package/exec

Or do you want to run commands? Open another window or run node server in background. Eg: nohup node myServer.js &

1
KillCoffee•

readline is a built in module that is used for this. You can prompt the user for input without blocking your server. You might also want the colour module.

var readline = require('readline');
var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

var ask_question = function() {
    rl.question("ya repeating question thing ", function(message) {
        //ya other thing
        ask_question();
    });
};
ask_question();
1
LumpySpoonOP•

Thanks! Will look in to it!

1