Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

raw_input alternative for node.js

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

like image 868
C1D Avatar asked Mar 27 '26 01:03

C1D


2 Answers

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();
like image 153
Peter Lyons Avatar answered Mar 29 '26 15:03

Peter Lyons


This page explains exactly what you need

like image 20
Bob Claerhout Avatar answered Mar 29 '26 14:03

Bob Claerhout