Restart NodeJS server with every change in the index?

1

I have mounted a Parse Server on an ubuntu, and every time I add a modification to the index.js I have to restart, which takes 4 or 5 seconds to come back to life.

Is there a more optimal way to add changes without dropping the server?

    
asked by Pablo Cegarra 10.07.2016 в 12:56
source

2 answers

2

The answer is yes and no

For the example you asked (the connection to the base) there is a solution. Instead of having your index.js like this:

var dbMotor = require('my-sql'); // o require('pg-promise-strict'); 
var config = require('./config.json');
var dbOpen = dbMotor.connect(config.parametrosDb);

var app = require('express')();

app.get('/data', function(req, res){
   dbOpen.query("SELECT ALL ... ", function(err, data){
      res.end(data);
   });
});

you can have a database wrapper that only restarts that part:

var dbMotor = require('my-sql'); // o require('pg-promise-strict'); 
var config;
var dbOpen;

var app = require('express')();

function start(){
  config = require('./config.json');
  dbOpen = dbMotor.connect(config.parametrosDb);
}

start(); // lee todo al inciar el servidor

app.get('/reset', function(req, res){
  start(); // vuelve a leer todo al resetear
  res.end("reset ok at "+new Date());
});

app.get('/data', function(req, res){
  dbOpen.query("SELECT ALL ... ", function(err, data){
    res.end(data);
  });
});

Analysis of the response

  • It is a simplification (many things are missing, the listen, closing the database if it is going to reopen, etc)
  • The advantage is that you do not have to cut the execution of node index.js
  • The important thing to note is that yes or yes you have to re-execute start you have to see how much of the 4 or 5 seconds it takes tasks to be done again
  • In this simplification it is necessary to call /reset from the browser, you can choose other activators, for example a watchFile that is observing if there were modifications in the configuration file.
  • Another alternative is to have a start for each type of thing, one for the database, another to read disk templates, another to change ports, etc.
  • Conclusion

    There is no magic solution , everything has to be tested and programmed, someone pays for the cost , the programmer programming, the programmer waiting for it to start at each restart, the user with the low performance (of something that is all the time seeing if it changed), the user with the probability that something has an error.

        
    answered by 10.07.2016 / 15:35
    source
    2

    install forever

    sudo npm install forever --global
    

    and then start your app (in debug mode) with:

    forever -f -w index.js
    

    then if you make any change in your index.js you will have an exit like this

    error: restarting script because change changed
    error: Forever detected script was killed by signal: SIGKILL
    error: Script restart attempt #1
    

    the flag -w tells node to be aware of any change in the index.js file and to restart it, interpreting it as an error but it is not.

        
    answered by 10.07.2016 в 22:43