Node.js - Serverside Command Promp Commands input? - javascript

I´m writing a Web MMO with Node.js and for debug and maintenance reasons i would love to have the ability to open my SSH connection, where i also start my server, and write a command like "logout all".
So what are my options to get command prompt input and use it while my server runs?
Example:
I start my server with "node app.js" - server starts.
Now i want to write something into the command prompt - how do i get said input so i can use it in my code?
I was trying to read into node.js-readline but i cant seem to find much about it and everything that i found about it needs a "question" to be asked so it can get the input.
I would love to have something like this:
var command = getCommandlineInput(); //command = "logout all"
It need to get any input any time. A callback function would be good aswell, so i can direktly run it throu a checkCommand() function.

So i just figured out how to do this:
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
readline.on('line', (input) => {
useCommand(input);
});
function useCommand (input)
{
switch (input) {
case "show userlist":
CMD_showUserlist();
break;
}
}
First we initiate readline.
With readline.on we listen for any input you type into the command prompt.
useCommand() takes this input and chose what function to run.
How to work with the given input string is up to you, in my example i just made a hard coded command "show userlist".

Related

Run a script before Command Prompt stops

I have a Discord Bot that edits a message every second. So if the bot goes off, the message will display the last edit.
I would like to, before closing the Node JS process (using Ctrl + C, closing the Command Prompt or any type of closing the process), edit the message to say something like: "The bot is off".
Example:
// something using discord.js
bot.on("close", myFunction);
// or in node, like in browser 'window.onbeforeunload'
node.beforecloseprocess = myFunction;
If theres a way with discord.js, that would be cool and resolve my problem, but if there a way of using Node Js it self or something in the Command Prompt would be better for future projects aside discord.js.
I would use the discord.js event 'close' and call a function that would change the message to 'The bot is off'.
also:
process.on('SIGINT', function() {
console.log("Closing")
}

i want to send message without Enterkey in node.js

AndroidPhone---------Raspberry Pi ------------Arduino
(server) (server) (bluetooth)
(bluetooth)
Now i can send message(0or1) from Android to Raspberry Pi by app
and i want to send this message to Arduino
However, when i play this code, message is not sent by bluetooth.
I think (stdin.process.on) is need input by enter.
but i cant. please help me
if (req.payload.toString() === '0') {
console.log('0');
process.stdin.on('data', function(data) {
var buf1 = Buffer.from(data);
serial.write(buf1,fuction(err, bytesWritten) {
if (err) console.log(err);
});
};
serial.on('data',function(data){ console.log('Received'+data);
});
If your code was copied and pasted, you should fix that misspelled "fuction" on line 5 first.
Edit:
i want to know how to use stdin.on without console input
Sorry, I don't know much about messaging between devices but process.stdin is meant specifically to read standard input, which means either console input or messages piped to your node process from another process' stdout.

Protractor Test with oracle Db

I requirement to test the DB alongside my angular app. The scenario is such that I do something on UI and see if the value persists in the DB(Oracle)
For this purpose, I installed node oracle DB package and was able to run a simple SELECT statement using the command "Node filename.js". The config and the select statement were present in the filename.js
Next, I tried to include the same steps in my step definition. The first step of my test case navigates to the application home page and the next step is to run a simple select statement. I have no asserts as I wanted to first get a basic query working
Here is my step def
let oracledb = require('oracledb');
let dbConfig = require('../configs/dbConfig');
Given(/^I run a random select query$/, function() {
let sql2 = "Select * from tablename Where columname = 8888901";
oracledb.getConnection(dbConfig).then(function(connection) {
//return console.log('Connected to database');
return connection.execute(sql2).then(function(result, err) {
return console.log(result.rows.toString());
});
});
});
When the test runs the browser opens and the home page is loaded and then the test is passed. I don't think the 2nd step is even run as the query result is not printed in the console.
Could someone please point me in the right direction. Can protractor work with OracleDB? have I written the test correct? I have so many questions regarding this and there is not a single online sample for a protractor test with oracledb. Any inputs here will be helpful, thanks

Input through node JS in Terminal

I am trying to make a program in Node.JS that will display some text, using console.log("");, then wait for the user to input some commands. First of all, I want to run this through the Linux Terminal on Cloud9 IDE, which does not pause long enough to input anything. Second of all, I want it to be like its own little command line. (I mean respond to certain case-sensitive commands, and ignore anything else.) Can anyone help with this?
Check out prompt. https://www.npmjs.com/package/prompt It works like:
var prompt = require('prompt');
prompt.start();
prompt.get(['hello'], function (err, result) {
console.log('you typed ' + result.hello);
});
Will do:
$ nodejs prompt.js
prompt: hello: world
you typed world
Happy coding ^^

Javascript equivalent of bash read -p

In order to get familiar with javascript and eventually explore the possible to restify the js script, i'm trying to convert a bash script into a javascript (with the intent on running the js the same way i'd run the bash script). So far i've been able to replicate most of bash functionnality in js except for parameter prompts.
For example, in bash I prompt the script user for parameter this way:
read -p "Are you a god? (Y/N) " ANSWER
with the effect that whatever input the user enter will be stored in variable ANSWER
from the i can continue with my script with something like this:
if [ "$ANSWER" = "Y" ]; then echo "You lying puny human"; else echo "Then die!"; fi
Now, i've looked at various options that javascript offer like the readline module or the prompt module but unless i'm missing something from the examples provided in the documentation
https://nodejs.org/api/readline.html#readline_readline for readline and
https://github.com/flatiron/prompt for prompt
It looks like it would do what i'm looking for but when i try that in an actual script, i can't seem to get my variable outside of the scope of the prompt/readline function
for example if i try this:
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('What do you think of Node.js? ', (answer) => {
// TODO: Log the answer in a database
console.log('Thank you for your valuable feedback:', answer);
rl.close();
});
echo(answer);
i'll get an error saying that the answer variable used in the echo is not declared
if i try this instead:
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var result
rl.question('What do you think of Node.js? ', (answer) => {
result = answer
console.log('Thank you for your valuable feedback:', answer);
rl.close();
});
echo(result);
the echo will be resolved when the prompt occurs and i'll see an undefined being echoed on my prompt line and then i'll be able to provide a value to the prompt and then once the prompt is resolved the script will end
My examples are using readline module but i pretty much got the same results with prompt
so to resummarize my question after this lenghty context:
can i get somewhat the same behavior in javascript that i get in bash using read -p in the sense that i have some script being run, then it pause to prompt the user then it carry on with the rest of the script?

Categories