0

I'm trying to take simple input from a linux shell when a javascript program is run. I've tried using readline() and prompt() but both of those throw Reference Error: readline() is not defined or prompt() is not defined.

//Decode Bluetooth Packets

var advlib = require('advlib');
console.log("What data to process - If you respond N than what is written inline will be decoded");
var input = require();
if (input != "N") {

    var rawHexPacket = input
    var processedpacket = advlib.ble.process(rawHexPacket);
    console.log(JSON.stringify(processedpacket,null, " "));
}
else {  
    //Put in raw data here!
    var rawHexPacket = 'dfasdfasdfasd4654df3asd3fa3s5d4f65a4sdf64asdf';
    var processedpacket = advlib.ble.process(rawHexPacket);
    console.log(JSON.stringify(processedpacket,null, " "));
}

So what is a simple way to get javascript input through a linux shell?

Emerson
  • 23
  • 1
  • 6

1 Answers1

0

I used the link posted and turned it into this (which works):

var advlib = require('advlib');

var readline = require('readline');

var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
try {
    rl.question("What data to process - If you respond N then what is written inline will be decoded. ", function(answer) {
        console.log("You said: ",answer);
        if (answer != "N") {

            var rawHexPacket = answer
            var processedpacket = advlib.ble.process(rawHexPacket);
            console.log(JSON.stringify(processedpacket,null, " "));

        }
        else {  
            //Put in raw data here!
            var rawHexPacket = '';
            var processedpacket = advlib.ble.process(rawHexPacket);
            console.log(JSON.stringify(processedpacket,null, " "));

        }
    });
}
catch(err) {
    console.log("Somthing went wrong - was your input valid?");
};
Emerson
  • 23
  • 1
  • 6