Interract with a .exe program with js instead of typing in the program - node.js

Hello I'm tryna make a Skyrim server Dashboard.
The server look like this =>
On this server i can type some command like this =>
when I manualy wrote /help and it show the output.
I tried to run the executable in node js, the server is working, I can join it, And I can see the output on my VSCode Terminal
But I can't input some text or command
Hope you can help me thanks in advance.
##Its my first ask

Add shell: true to the spawn method so you can still pass commands to the process.
const child = require('child_process').spawn("C:/SkyrimTogetherServer.exe", {
shell: true
});

Related

Github Cli Command does not work with express js

In my backend express server , I am trying to execute a github cli command using execSync. But it hungs up there. The command is following:
`
execSsnc("gh auth login --web")
`
In the same machine, when I use terminal to execute the same command, it works fine. In fact, I have tried a executing this line of code in a simple nodejs project and this too works fine. But whenever I use this in my express server, it hungs up there. I have tried using spawn, exec, spawnSync too but no luck so far.
Also when I use execSync("gh auth status") this gives error saying there is no github hosts currently. But in the machine, when I run this command from the terminal, it says that I am already logged it using Personal Access Token. Thus I found that running the login command from the express server is the main issue. Is there any way out?
First, check if setting GH_DEBUG to 1 or api generates any verbose log which might shed some light to the issue.
const env = {
'env': {
'GH_DEBUG': 'api',
...process.env
}
}
exec('gh auth login --web', env, function (error, stdout, stderr){
console.log(stdout, stderr, error);
});
Second, check with another exec who you are when executed from Express.js, and compare it to your command-line environment (where it does work)
Using gh auth login --token < aTokenFile (passing the token as stdin to your execSync call) might be a better alternative than --web.
Note that execSync create a new shell and executes gh command in this shell.
Depending on the OS, the shell used by execSync might be different than the shell used in your terminal (bash/sh) or miss some env variables that are available in your terminal. This might explain why "gh auth status" execution shows different results.
Try to verify if the environment variables is your Express app are different than environment variables in your terminal

Run another Node JS file in command prompt without closing and restarting it

Being New in NodeJS Whenever I run my node JS file in command prompt. I have to close the console window and type the same procedure again. is there any way that i have to not run again and again command prompt.
Use ctrl+c in command prompt and you can use your same command prompt window to run the same program.
For example there are two node.js files.
nodeOne.js
nodeTwo.js
If, these nodes need a port to run, configure both on different port numbers.
e.g. 8080 for nodeOne.js and 8081 for nodeTwo.js
Now, open two instances of 'cmd' and execute both nodes separately.
I'd use "start node whatever.js".
That'll open it up in a separate window, which you can kill whenever and just hit Up arrow on the original cmd window to run the same command.
On the off chance, you're killing the original cmd just to restart node, ctrl-c a couple times should kill it, shouldn't it?
Then up arrow and you've got the last command again.
This could use a little more context, but I assume you are running something like this on the command line:
node my_file.js
Or just:
./my_file.js
Whenever you write command line scripts like this, it's good to call process.exit to tell Node when you're done. So, for example:
#!/usr/bin/env node
function myFunction () {
// Do some stuff that takes a while...
return Promise.resolve()
}
myFunction()
.then(result => {
console.log(result)
process.exit(0)
})
.catch(error => {
console.error(error)
process.exit(1)
})
And if your script is just hanging and not stopping, you can always hit Ctrl + C to abort.

node.js child process change a directory and run the process

I try to run external application in node.js with child process like the following
var cp = require("child_process");
cp.exec("cd "+path+" && ./run.sh",function(error,stdout,stderr){
})
However when I try to run it stuck, without entering the callback
run.sh starts a server, when I execute it with cp.exec I expect it run asynchronously, such that my application doesn't wait until server termination. In callback I want to work with server.
Please help me to solve this.
cp.exec get the working directory in parameter options
http://nodejs.org/docs/latest/api/child_process.html#child_process_child_process_exec_command_options_callback
Use
var cp = require("child_process");
cp.exec("./run.sh", {cwd: path}, function(error,stdout,stderr){
});
for running script in the "path" directory.
The quotes are interpreted by the shell, you cannot see them if you just look at ps output.

Why nodejs console.log only showing " ... "?

I have this in helloworld.js :
console.log('Hello World');
and the output is :
http://img856.imageshack.us/img856/4152/8vh3.png
You're currently using REPL of node.js.
You should run such command directly in CMD.
By the way, you can input raw code, like console.log('Hello') in REPL.
This should be run directly from command line interface as mentioned by #Chichou. If you want to test such things you can also use browser console.

debugging node.js child_process fork example on IntelliJ IDEA

I am trying to debug the child_process example from here using IntelliJ IDEA 12.1.3 and node 10.10. When I run nodejs app.js from a terminal everything works. The console output displays as expected. However, when I debug the same script using IDEA there are no messages in console output and the app just sits there. This is what is in the console window:
/usr/bin/nodejs --debug-brk=58954 app.js
debugger listening on port 58954
debugger listening on port 58954
When I run the script in IDEA without the debugger attached, the script works as expected.
Why does attaching the debugger break the script?
You can force the children to use a free port for debugging. InteliJ will automatically pick up the port chosen by the child process.
Here's an example:
// Determine if in debug mode.
// If so, pass in a debug-brk option manually, without specifying port.
var startOpts = {};
var isInDebugMode = typeof v8debug === 'object';
if(isInDebugMode) {
startOpts = {execArgv: ['--debug-brk']};
}
child_process.fork('./some_module.js', startArgs, startOpts);
looks like a bug in node.js fork to me: both parent and child processes receive --debug-brk=58954 switch and attempt to start debugger and listen port 58954.

Resources