Input through node JS in Terminal - javascript

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 ^^

Related

child_process cant input after some time

I am working on a live terminal in the browser over socket.io. And I encountered a problem.
My code:
child = require('child_process').spawn('cli opening command'),
//simple output to the client
child.stdout.on('data', function(data) {
io.emit('consoleOut', data);
});
//socket
io.on('connection', (socket) => {
socket.on('consoleInput', (msg) => {
//writing the message to the stdin stream
child.stdin.write(msg + '\n');
})
});
The Problem:
With this setup, the output is always "No such command '[user input]'..." only on the first input after the CLI was started it is working fine. And when I look at the output, I see that the first letter between the two quotes is an ASCII decimal 4 that equals to an end of transmission character.
Example (if I look at the string in the browser):
No such command '\u0004set verbose 5'
And the weird thing is when I write to the stream and hardcode it, the commands are perfectly fine and get executed like they should. But if then a user inputs his command, again every command is not working, even the first one.
child.stdin.write('set verbose 5\n');
child.stdin.write('set verbose 3\n');
I can verify that it is not the user input because when I change the socket on the consoleInput function into:
child.stdin.write('test' + msg + '\n');
I still get the same output (if for example the user inputs set verbose 5):
No such command '\u0004testset verbose 5'
(of course the command wouldn't work in the first place, but it's to show for the EOT chat)
My guess:
I get the EOT char only when I write to the buffer, and my string includes at least one \n.
Then everything until the last \n gets sliced out of the buffer, and there is then a EOT char left behind.
And It makes sense if this is true that I can input one command at the start that is working fine because there the whole buffer is empty and no EOT char is there.
Thanks, Julian ;)

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")
}

Node.js - Serverside Command Promp Commands input?

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".

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.

Javascript Logging function

For a little project I'm coding besides my job I wanted to make a log function so I can output simple things into a textfile like the user input and possible errors that occured.
I wanted to make this log a simple .txt.
The problems I ran into were the following:
I want to check if the file already exists if not, create a new one;
Then when the file exists I want to write below the existing contents.
This is what I got so far:
/*
Form:
\r\n
\r\n *** logged (<dd.mm.yyyy> || <hh:mm:ss>) ***
\r\n
\r\n <pMessage>
\r\n
\r\n *** log end ***
*/
function log(pMessage)
{
var logPath = "../../Logs/SiteLog.txt";
}
I know it is not much because I haven't done anything there yet. So i basically need three things: Checking if the file exists, creating a file, editing the file.
Thanks in advance for your help.
If this is client side, the browser will prevent you from writing to the file system. If this is strictly for your own testing purposes locally, you could write to window.localStorage
https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
This would allow you to read and write from a local storage that the browser caches based on domain
function read (name) {
return window.localStorage.getItem(name)
}
function write (name, value) {
var report = window.localStorage.getItem(name) + '\n' + value
window.localStorage.setItem(name, report)
return report
}

Categories