How to reload one file on Express without restarting? How to reload one file on Express without restarting? express express

How to reload one file on Express without restarting?


You can use require-reload to do that. You should have a file where you import the router.js module, to which you are appending new code, that should look like:

var reload = require('require-reload')(require),var router = reload('router.js');

Each time you append new code you should do:

try {    router = reload('router.js');} catch (e) {    console.error("Failed to reload router.js! Error: ", e);}

The express-route-refresh module may come in handy too. In fact, internally it also uses require-reload.