3

I am creating a server application and I would like to be able to write commands like start server, stop server, broadcast("clients","Hello") in to my application but I can't figure out how to do it in node.

I do know how it would look inside python.

while on == 1:
    cmd = raw_input("user> ")
    if cmd == "start server":
        startserver()
    elif cmd == "stop server":
        stopserver()
        on = 0

I have seen some npm modules for this but they require me to write arguments, I want to make this a console type application.

Thanks

1
  • Thanks for referencing python. Only for that reason I stumbled upon this answer! Commented Jan 21, 2020 at 19:08

2 Answers 2

5

Here's a snippet that was recently removed from commander.js. Something along these lines should work.

process.stdout.write("user> ");
process.stdin.setEncoding('utf8');
process.stdin.once('data', function(val){
    //look at val here and execute startserver() et al accordingly
}).resume();
Sign up to request clarification or add additional context in comments.

3 Comments

That works but I have one problem, after I execute a command I want to be able to execute another, and for some reason it wont work if I put it in a loop, when I do it just spam's user >.
I fixed my problem by calling the function with in the function. Thanks :)
I believe process.stdin is initially paused so the resume starts it.
1

This page explains exactly what you need

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.