how to change node.js variables from terminal - node.js

I'm trying to run a nodeJS service that contains some parameters, then once it is running, stores a lot of session variables.
My script shouldn't be restarted because of this, all these working variables should remain the same.
Is it possible to change some global variables inside the running nodejs script while it is running? I was hoping the node would actually be a console where I can run JS in it but it appear it is not, it only outputs the console.log() method and errors, there's no input.
I know I could create a method inside my script to change these variables, but there are many, and I dont want to create a function that can handle them all because this would be very insecure.
Is there something I've missed about the node console inside terminal?
In fine, the script will be running with 'forever'

What do you mean no input?
node.js can read from the terminal like any other technology.
You can look at: https://nodejs.org/api/readline.html
They have a simple example:
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("What do you think of Node.js? ", function(answer) {
// TODO: Log the answer in a database
console.log("Thank you for your valuable feedback:", answer);
rl.close();
});
I wrote a command line installer for our app using this.

Related

How to see process.env after starting server

I am developing sample apps and would like to know process.env variables,
I know console.log(process.env) will return its variables.
But can I see them after run its server ?
npm start
I couldn't input anything in console.
As I am new to node.js, will you please let me know.
by switing NODE_ENV, it seems that development,staging,production is switched.
So that I would like to comfirm them.
Thanks
If you start your server in docker and don't pass custom variables to process.env in your app, you can see your env by docker command:
docker exec your_container env
Yet another way - create a specific route in your application that will be return you all data from process.env.
Something like this:
GET yourserver/api/system/env
But this way is not secured and you should think about protection of your system route.
UPD
Also you can call console.log(process.env) after server has been started.
await app.listen(3000);
console.log(process.env);

Attach to current process on nodejs over ubuntu server terminal

I have a process running over a ubuntu server. I only have access on terminal.
I made this script:
index.js
let i = 0;
setInterval( () => {
i++;
console.log(`try ${i}`);
}, 1000);
I run with: node index.js &
Now I open a new terminal and I want to see the result on console.log.
How can I do it?
New edit:
The principal idea is send a console.log in a terminal and, recovery this console.log in another terminal. This is the goal. Recovery the console log in another terminal. How can I do it?
Of top of my head, I'd suggest going for a combination of https://www.npmjs.com/package/commander and some sort of IPC - redis-based,*mq or plain https://www.npmjs.com/package/node-ipc
If you don't have wide variety of commands you'd need to send to that process, you can also get away using signals, for example: SIGUSR1, SIGUSR2.

Node.js: Passenger puts stop to application because it takes too much time

I installed nodejs on my website via the cpanel Application "Setup Node.Js App" and created the startup-file "app.js".
When I execute this file in the terminal with node app.js it works quite well.
Anyway if I access the page in the browser with the URL I get the error "Website not available" (ERR_CONNECTION_CLOSED). Sometimes though I get to a different window which says:
The Phusion Passenger application server tried to start the web
application, but this took too much time, so Passenger put a stop to
that. The stdout/stderr output of the subprocess so far is:
Item: apple
Item: eggs
Item: bread
Item: milk
When I click on the details it shows me this
errormessage
.
The current code is
const readline = require('readline');
const fs = require('fs');
const myInterface = readline.createInterface({
input: fs.createReadStream("shoppingList.txt")
});
function printData(data) {
console.log(`Item: ${data}`);
}
myInterface.on("line", printData);
but so far, this happens with any code in app.js. Also when I only write:
console.log("test");
I don't know how to get the app working when accessing it by the URL.
Can someone please help me with this problem?

How to close angular server from nodejs?

I am making a web page using angular 6.0.8 with the cli too. I was able to successfully start the server using nodejs Child Process like this
var child=children.exec("ng serve",{},function(error,stdin,stdout){
console.log('server terminated: '+error);
});
Then when I try to close the server like this
child.kill("SIGKILL");
It cuts off the connection between the server and my nodejs program but the server doesnt actually close until the host nodejs program does even thought the child process now registers as killed. I also tried to close it by connecting to the readline of the child process but that seemed to just cause more errors. this is what I tried
var childrl=readline.createInterface({stdin:child.stdin,stdout:child.stdout});
childrl.write(null,{ctrl:true,name:"c"});
the error it gave me is that input.on is not a function.
Try using Node.JS's child_process to spawn a new process something like below:-
var angular = require('child_process').spawn('ng', ['serve']);
When you need to close the angular server, you may use something like below:-
angular.kill('SIGINT');
Hope it helps.

Viewing node console log remotely

I have been building my first node app. In testing on my mac, I was able to view the console log output using terminal.
I'm now moving the app to a server but I still want to get a live dump of the console log. Yes, I can get this by SSH'ing into the server - start the app then watch the output. But, say my SSH connection to the server gets disconnected. After re-connecting to the server, how do I go about viewing the terminal output of that process?
One solution I came across was http://console.re - this looks ideal, however it comes with warnings not to use in a production environment. Coupled with the fact that it's public, I'm hesitant to use it.
Does anyone know of an alternative solution similar to console.re?
Thanks
You could try using a custom function that writes the output to a log file, as well as printing it on screen.
Something like this: (note that this won't accept multiple arguments)
var fs = require('fs');
module.exports = function(text) {
fs.writeFile('console.log', text, {
flag: 'a' // append
}, function(){}); // ignore the response
console.log(text);
};
Perhaps screen, tmux, or similar software might work for you.

Resources