Search code examples
node.jsterminalforever

how to change node.js variables from terminal


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'


Solution

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