There are already node "wrappers" that handle watching for file changes and restarting your application (such as node-supervisor), as well as reloading on crash, but I wasn't fond of having that. Piping adds "hot reloading" functionality to node, watching all your project files and reloading when anything changes, without requiring a "wrapper" binary.
Piping uses the node cluster API to spawn your application in a thread and then kill/reload it when necessary. Piping should not be used in production.
npm install piping
Piping is super simple to integrate and should not change the way you run your application:
;// Application logic hereexpress = ;app = ;app;
With the default settings, this will cause a second instance of your application to be launched, which is monitored by the first. Piping then does a trick with uncaughtException handling to avoid running any of your code on the first process.
The function returned by piping also accepts an options object. The following options are supported:
process.argv, which should be sufficient provided you launch your application via "node yourapp.js". Other launch methods may require this to be set manually. If your app doesn't reload/reloads when it shouldn't, try changing this.
main: "./app/server.js" hook:true;// App logic
Piping can also be used just by passing a string. In this case, the string is taken to be the "main" option:
"./app/server.js";// App logic
Piping emits lifecycle events on both the supervisor and your application process, allowing for custom behavior. Most events provide the reloaders status, in the form:
exiting: boolean // True if the process is about to reloadfirstRun: boolean // True on the first run of the applicationexitReason: string? // Reason for last exit, one of "exited", "errored", "killed", "requested"fileChanged: string? // Name of file which changed and triggered the last reload.
You can receive lifecycle events by passing in a function as the second parameter to the piping call, or the first if you are not specifying any options. This function will be called once everything is ready, and will be passed an EventEmitter. See the example below for supported events:
On the application process, the piping call will return an EventEmitter. See the example below for supported events:
const reloader = ;reloader;reloader;