Can I send commands using node.js? - javascript

I'm running a node.js server using Terminal (Mac) and want to be able to emit messages to the client via the command line. Is this possible? I could code the messages part of it no problem, it's more how I can send something directly from the server to the client rather than from the client-server-client relationship I only understand at the moment.

This is probably what you're after if not let me know and I'll delete answer.
Look into a module called Commander found here.
It will allow you to build up a cli that you can then hook into your methods/prototypes etc.
For Matt :)
var program = require('commander');
program.option('-s, --send', 'Send command');

Related

How to restart a Node.js project?

I'm not a Node.js developer. So I have no idea how it works. I've been a PHP developer for over 8 years.
Because of some reason, I need to make a small change in a Node.js project which is live. All I have to do is changing a payment gateway token. I did it like this:
After pulling it on the server, users still go to the old payment gateway. So I guess I need to do a restart. (I'm saying so because, for PHP projects, when you change a config-related thing, you need to restart PHP).
Not sure should I restart what thing? Noted that, the server is Ubuntu 20.04 and uses Nginx to talk to Node.js. In other word, how can I see Node is running as what service on Linux?
Also, there are two files that I think I need to run the project again after restarting Node through one of them: index.js, server.js. Am I right?
And
Your Node.js script likely runs under a process that restarts the script in case it dies. There are several "run forever" wrappers, the most popular one is pm2. Find out which one is used in your project. Try pm2 list as the user your project executes under. If pm2 type pm2 restart app_name to restart your project.
Please check if it is a node.js project so you can write the command node index.js or node server.js with this command you can start your node server.

Connect to OpenVPN server through Node.js

I’m trying to create a GUI client for connecting to OpenVPN servers using electron and node but I’m struggling to figure out how to actually connect to the servers using the .ovpn files.
My question is what is the best way to connect to an OpenVPN server using node? Would it be best to Tun terminal commands like
“openvpn—config path to config”
Or is there another way applications like tunnelblick do it that might be easier or more efficient?
Hello I have been working with electron and ovpn on my last project, so here are a few tips.
VPNs require admin/root privilege in order to get setup, so running child_process.spawn on openvpn --config <path> will fail unless your electron app is being ran via sudo/admin privilege.
You can also use electron-sudo package, link here. This is basically a child process spawn with sudo/admin. Aka, app runs normally, but vpn command runs with sudo.
However, if your client is sketchy about giving you sudo/admin, the VPN must be ran separately prior to launching your app.
All in all its a admin/sudo thing.
Hope this helps.

Can programs be written with JavaScript that run Windows 8 terminal commands when a gui button is clicked?

I know Windows 8 'apps' can be developed using web technologies but I haven't been able to find out if terminal commands can be run in the background using web technologies as an interface. I basically have mongoDB on my computer and it takes two terminal windows open to run it. I thought it might be a neat project to see if I could write a little app that is nothing more than a button that launches both commands behind the scenes saving me the hassle of going to the directories and running the commands manually for both terminal windows.
If you plan to launch apps via server-side JavaScript (e.g. node.js), use the child_process module..
The workflow would be that in the windows 8 gui side, it will just issue a request to your own local server in node.js, then it would execute those commands.
Example:
var exec = require('child_process').exec;
var child = exec("insert command here", function(err, stdout, stderr) { });
See examples exec and spawn for more examples.
======
Another thing you can do is create a batch (.bat) file that contains those two commands needed for your mongodb instance and put that as a shortcut in the Windows 8 Start Screen.
It depends on what kinds of commands you need to execute, and when and where. If you plan to execute commands remotely, I'd assume server-side JS would be appropriate, but if you plan to execute commands locally, I think all you need is just batch scripting.

Capturing console output in node.js with out editing server code

First stackoverflow question! I am trying to capture all console.log() output from a node.js server. Assume I am on a linux machine and I cannot edit the server's code, (project leader's request). I can, however change how I start the server (node --debug server.js) and I can attach shell scripts. My intent is to pipe the console output into another socket.io server.
I am thinking the only obvious solutions are to either write a linux command to pipe output of the server into something or node.js has a tricky --debug method to output the information.
End result, I want to be able to run the server and pipe the console output to a API testing server. That way, a tester would be able to see, streaming, the output of the server in correlation to the response JSON objects from the API end points.
Thanks, I would love to hear your solutions...
Answered over IRC, if anyone's interested:
<shesek> patrickm, can't you just pipe the output to another script?
<shesek> node server.js | node send-logs-to-somewhere.js
<shesek> patrickm, in send-logs-to-somewhere.js, you can just read from process.stdin
<patrickm> :)
<patrickm> thats easy

Writing a pseudo terminal in python for a web based ubuntu terminal

I am interested in writing a web based terminal which can execute and autocomplete the commands in ubuntu terminal.
I have fiddled with Anyterm http://www.anyterm.org which uses ROTE: "a simple C library for VT102 terminal emulation" at the backend server and javascript/html for the terminal ui. The problem was that I couldn't have it open two terminals in the same window.
After doing some research I've decided to write a terminal server/pseudo terminal (whichever is the right terminology) in python which can:
execute commands in ubuntu terminal (such as ls, grep etc...)
autocomplete the commands (like $gedi (pressed tab) $gedit)
and have multiple instances of terminal open at the same time (when typed ls in one, the other terminal won't recognize ls as the last command since its another terminal session)
I have found that with pty python module I can write a pseudo terminal
however I'm new to python and I couldn't even get the example on that page to work.
I'm planning to have the python script serve a http server with the javascript/html terminal ui I would write (this was the main reason I wanted to choose python for this project). Then I want to let the web ui talk to the backend terminal "server" and get the results back to the web ui.
If you could point me in the right direction, maybe tell me which module I should use -if not pty- and give me some ideas on how to write the python pseudo terminal server I would appreciate it.
I know javascript/php and familiar with bash scripting- At this point I could go for a C based or python based backend server, is python right for this project?
Note: I'm planning to write a ui that uses ajax post or get methods to retrieve the terminal output from backend server.
You need to learn the basic terminology first. The shell is the program that interprets your input command lines, such as grep "foo" abc.txt. A terminal emulator is a program that mimics a terminal. Terminal is a device with display and keyboard that one
used in 1970s to access a UNIX mainframe. A pseudoterminal, pty is the device node supporting terminal emulators, as opposed to "real terminal devices" (tty1) for example.
Now, you could either build a shell, that would work in terminal instead of bash or dash; or you could build a terminal emulator that is usable over the internet; you could run any existing shell in it. Both are challenging tasks by themselves.
Python documentation is not of much use, I recommend you should start by googling more of these concepts first - and not only python references, but C, C++ too.
And lastly, if you want to run Emacs or nano or some other advanced program in your shell, you want to use the pty module.
I had a friends who did something similar but he did it over Google chat, I don't know how much help it will be but take a look at xmpp (python modual) maybe it could help you, or even here is a link to his source code:
chatIO
I didn't work with him on it but it was really easy for him to use
Good Luck

Categories