Auto reload express app without restarting the process or the HTTP server.
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('hello world');
});
app.listen(8080);
If you change hello world
to hello beautiful
the change won't take effect unless you restart the process. Express LoadApp automatically reload your code everytime it changes.
const express = require('express');
const loadapp = require('express-loadapp');
const app = express();
loadapp(app, require.resolve('./app'));
app.listen(8080);
// app.js
module.exports = (app) => {
app.get('/', (req, res) => {
res.send('hello world');
});
}
Now everytime app.js and any other file referenced by app.js is changed the changes will take effect immediately when you access the server.
The difference between this library and other auto-reload library is that this library doesn't perform auto-reload by restarting the process or server but it re-require your code/module (after deleting the caches). Read index.js for the implementation.
loadapp(expressApp, fullPathToFile, enableAutoReload=true);
expressApp
is the thing you create from callingexpress()
.fullPathToFile
is the "main" file to be autoreloaded. Writing full path as/home/myname/myproject/myfile.js
is super dumb so it is recommended to write it asrequire.resolve('./myfile')
.- The "main" file must exports a function that has one parameter which is
expressApp
. Use it to write middlewares and routes and everything. Read example/app.js for an example. enableAutoReload
should be false in production environment. This library re-require the "main" code which practically load the whole app and it isn't wise to do it in every request when in production. You can do something like this (can be seen in example/index.js):
const app = express();
loadapp(app, require.resolve('./app'), app.get('env') !== 'production');
Just copy the file. Not on npm (yet) because it's so darn simple I don't think it's "package" worthy.