1

This is a class project, the teacher will accept only one (.js) file as the submit. In the project, I will need to read command from the console and react to it. Is that possible in js? I have tried event, however it seems like must base on html components.

Robert Columbia
  • 6,012
  • 14
  • 28
  • 36
Hanrun Li
  • 23
  • 1
  • 8
  • Sure. You have to run JS on the console and read the standard input. The Node docs cover most of that, I believe. – ssube Sep 14 '16 at 17:39
  • 2
    So this is talking about something he can run the browser? Or is this something that can be run with Node? – arjabbar Sep 14 '16 at 17:40
  • So I need to use Node.js is that right? – Hanrun Li Sep 14 '16 at 17:42
  • On Windows the Scripting Host can do this, E.g. http://stackoverflow.com/questions/4441003/how-can-i-write-a-simple-jscript-input-output-program – Alex K. Sep 14 '16 at 17:44

1 Answers1

0

The read input from the command line, the prompt module of nodejs is nice :

var prompt = require('prompt')

process.on('uncaughtException', function (err) {
    log.error(err.stack)
    process.exit(1)
})

prompt.message = "> ".green;
prompt.delimiter = "";

var properties = [{
    name: 'login',
    message: 'login: '.white
}];

function onErr(err) {
    console.log(err);
    return 1;
}

function start () {

    prompt.start();

    prompt.get(properties, function (err, input) {
        if (err) {
            return onErr(err);
        }
        console.log("hello",input.login)
    });
}

start()

see https://www.npmjs.com/package/prompt

Flint
  • 1,483
  • 1
  • 14
  • 24