How to write all socket.io server console text into txt file? - javascript

My socket server shuts down sometimes and I can't check why because there is no txt log where i could check last error messages, maybe there is way to write it in txt file after shutting down?

Invoke your program in the following way: node myserver.js 2> error_log.txt
When the program closes the error_log txt will have all messages, including the error message that made the program crash written in it.
To also get all console.log messages you can do node myserver.js > error_log.txt > 2>&1
(I used this command in the bash console on windows)

Related

Open terminal from JavaScript and run bash command Linux

I am trying to run a bash file.
I can open the terminal and write bash run.sh and it works fine. But I am trying to do this via JavaScript. I am trying to open the terminal and run the command in that terminal.
let extExecCommand = `bash runtheme.sh`;
childProcess.spawnSync(extExecCommand, {shell: true});
Content of run.sh is this
#!/usr/bin/env bash
#This is a comment
#defining a variable
export FLASK_APP=script.py
export FLASK_DEBUG=1
flask run
xdg-open http://127.0.0.1:5000/
Actually, I want to run the flask app from a button click on the website.
Currently I am doing this, it will run the flask app but I can't kill the child process. I need to be able to run this app from a button click and need to be able to close it. As we can by pressing ctrl+c in a terminal window. So is there any way I can open the terminal window from JavaScript and run "bash run.sh" in that window?
I also tried making run.sh executable but it doesn't open the address in window automatically.
Javascript is client side, which means your code will only run on the clients browser sandbox. So for obvious security reasons you can't execute shell commands on the clients machine. You can run it from your own, local machine though. So here is my code if you want to do that:
const execSync = require('child_process').execSync;
const result = execSync('bash /path/to/executable.sh', {encoding: 'utf-8'});
console.log(result)
If you want to read more about sandboxes, i recommend this article which made things for me more clear: Sandboxes Explained

How to run my node.js application through /etc/rc.local?

I want to run a node application from my raspberry pi. The application is supposed to start on boot.
I have included the following lines in /etc/rc.local (before exit 0) :
cd /home/pi/PPBot
node bot.js > dev/null &
I first navigate to the correct folder and then run the bot from there. However the node application is not running when I reboot my raspberry pi. So it seems that rc.local is not executed or unable to execute the lines I provided.
I am looking for a solution so that the application will run at boot.
save this
[Unit]
Description=Node JS Script Service
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/node /path/to/hello_env.js
Restart=on-failure
[Install]
WantedBy=multi-user.target
to /etc/systemd/system/ as nodescript.service
sudo systemctl daemon-reload
sudo systemctl start nodescript
if it worked, make it startup on boot
sudo systemctl enable nodescript
The examples I 've seen (like this one) used
cd /home/pi/PPBot
node bot.js < dev/null &
instead of
cd /home/pi/PPBot
node bot.js > dev/null &
Note the < instead of the >. As you can see here, the < redirects the Standard In (stdin), while the > redirects the Standard Output (stdout). And as described here,
< /dev/null is used to instantly send EOF to the program, so that it doesn't wait for input (/dev/null, the null device, is a special file that discards all data written to it, but reports that the write operation succeeded, and provides no data to any process that reads from it, yielding EOF immediately). & is a special type of command separator used to background the preceding process.
Since you havent used < dev/null, maybe your program is stuck and waits for some input. The & at the end makes your program run in the background. Try to not include it and see if your Rasberry Pi is still booting. If not, you know that your programs runs continously and blocks further booting; when you used the & sign, your program just failed in a separate process. In this case changing the > to < should help because your programs doesn't wait for input.
If the above doesn't work, update your question with the specific error message. You can view the boot log by using the command dmesg (display message).
In this case when you
cd /home/pi/PPBot
you are no longer in the root directory so when you run
node bot.js > dev/null &
it is looking for the dev folder at /home/pi/PPBot/dev
You will need to add a leading / to ensure it is accessing /dev
change
node bot.js > dev/null &
to
node bot.js > /dev/null &

Stream output to stdout after spawning `git clone`

I'm just trying to run git clone from node and stream the output into stdout just like running from shell normally would, but after using child_process.spawn, I can't get the output to pipe into stdout. Currently I'm using:
child = spawn('git', ['clone', url]);
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
But I only see "Cloning into 'directory'" message and not the remote messages and "Receiving objects...".
What am I doing wrong?
By default, git clone only shows progress when it's running in a terminal. When it's not running in a terminal progress can be enabled with the --progress argument:
Progress status is reported on the standard error stream by default when it is attached to a terminal, unless -q is specified. This flag forces progress status even if the standard error stream is not directed to a terminal.
However I'm not sure that that will do exactly what you expect. The progress output isn't simple output; values change in place. I'm not exactly sure how nicely that will play with child_process.spawn().
Note also that output is to STDERR, not STDOUT.

Node.js - Server crashes when started on startup, doesn't crash when manually started

I am running Node.js and Socket.io for online chat.
I have created a file in:
/etc/init/example.conf
it has two lines:
start on startup
exec forever start /var/www/example.com/html/server.js //location of server file.
Whenever I start file upload in chat application, it crashes but instantly restarts.
Whenever I kill node process though, and start it manually - it works fine.
I also can't get any logs or anything from terminal as when it's auto started - it doesn't print me anything to terminal.
I am still new to Node.js and Linux in general.
Node.js is running on Express + Jade.
How do I determine specific cause?
I managed to solve my problem, after a bit of searching around I found out about tail command.
My issue was a bit harder to trace because Node.js was a process started by autostart so when I launched terminal and connected to server, process was just running in background basically and I wouldn't get any output (including exception messages).
Anyway, solution that worked for me was:
I typed
ps aux | grep node //to find PID of node process
then I went to following directory
cd /proc/[pid of running node service]/fd
In fd directory there are few objects you can get output from but if you want to attach and listen to servers output including uncaught exceptions, you need 1.
So:
tail -f 1
that way I was able to cause website to crash and see the output.

CLI debugger for Mocha tests

I want to be able to drop a debugger statement (or something similar) right into my code and get a REPL when I run my tests so I can interact with my code to debug stuff.
I understand that Node (via V8) supports the debugger statement out of the box, but you have to run your code with certain flags (via node debug or node --debug or node --debug-brk -- I don't really understand the differences).
Mocha has these same flags as well, and when I run mocha debug I indeed get a debugger prompt in my terminal:
< Debugger listening on port 5858
connecting to port 5858... ok
break in node_modules/mocha/bin/_mocha:5
3 */
4
> 5 var program = require('commander')
6 , path = require('path')
7 , fs = require('fs')
debug>
But the breakpoint is in Mocha's code, not my breakpoint. This blog post says you're supposed to type run then continue to get to your breakpoint, but when I enter run it tells me "App is already running..." and when I enter continue it tells me "SyntaxError: Illegal continue statement".
It says "listening on port 5858" -- do I need to open a separate connection to that port from somewhere, like a browser or another terminal session?
I've seen some things about node-inspector, but I like to stay in the terminal as much as possible so I prefer a way to do this without opening a browser.
I'm new to JS, so please ELI5 :)
Also, if it's relevant, I'm actually using io.js, not Node proper
When the node/iojs debugger first starts up and connects it always breaks on the first line of whatever the script was that was passed into the interpreter.
Since you're already running your app, you do not need to type run here, but you can just type c to continue execution until your breakpoint has been it.
run is useful if the program terminates while you're in the debugger -- you can start it up again! (You can also do restart to restart the program within the debugger).

Categories

Resources