K
K
Konstantin Chuykov2015-05-29 06:55:03
Node.js
Konstantin Chuykov, 2015-05-29 06:55:03

How to make it possible to change application code without reloading the main nodejs application?

Hello! There is such a framework habrahabr.ru/post/194250 , and it has such an opportunity. I think that this is very convenient, since now, having made changes to the nodejs application, I have to restart the pm2 restart server project in the console to check the changes. How can I make sure that after saving the file, it goes into memory, or recompiles instantly, and I just perform operations in the browser?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
K
Konstantin Kitmanov, 2015-05-29
@chuikoffru

npm install -g nodemon ; nodemon app.js and you're done.

T
Timur Shemsedinov, 2015-05-29
@MarcusAurelius

As the author of the mentioned application server, I will clarify the situation. All other solutions given restart the processes. At best, they run in multi-process mode (using cluster or using child_process) and restart child processes (worker), while the parent process (master) watches for changes and does not restart. This really gives zero downtime, which is important for production, but not so important for development. And something else is important - all data is removed from memory, all the state deployed there, connections to the database are closed, libraries are unloaded, and everything that was initialized at the start of the application will be completely destroyed and rebooted. In Impress, separate files are loaded, replacing the previous code on the fly in memory. This is done without eval, using vm. With numerous changes, he understands that many files are changing (for example, a new version of the application is being uploaded), waits for this activity to stop, and then updates everything in one fell swoop. With frequent changes to the same file, a situation may arise when 2-3 copies of the same code will be in memory in parallel. The fact is that a stream of requests is entering the system, and the old requests were still running at the time when the previous version was, and they did not have time to complete. Therefore, you need to wait for their completion, and for new ones, you can already use the new version of the code. There are many more features that are too long to talk about. In general, I am not aware of other implementations of this solution. If they are found, then send for comparison. The above article is somewhat outdated, for the initial review, take this one:habrahabr.ru/post/247543

V
vsvladimir, 2015-05-29
@vsvladimir

Alternatively, you can load modules on the fly from your application if they have changed:

function reloadModule(module) {
    delete require.cache[require.resolve(module)];
    return require(module);
}

N
NETChaser, 2015-05-29
@NETChaser

like so
var _eval = require('eval');
var f1 = function () { return 321 };
console log(f1());
var f1 = _eval('module.exports = function () { return 123 }');;
console log(f1());

K
kotofey, 2015-05-29
@kotofey

I actively use Child Process , at the output I have just what you need.
With each new launch of the child process, it pulls up the current state of all scripts.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question