Automatically reload node.js files with Supervisor
One of the annoyances of Node.js for development purposes is the fact that you have to restart the server everytime you make changes to your included modules. This is because Node actually caches module includes for better performance. A great workaround is using the Node Module “Supervisor“.
Supervisor will monitor all of your included modules and restart the server automatically when changes are made.
Installing supervisor is easy with NPM (Node Package Manager)
npm install -g supervisor
This will install supervisor globally which is probably a smart idea. Invoking your server with supervisor is also really easy:
supervisor server.js
Here is an example of the startup output:
DEBUG: Running node-supervisor with DEBUG: program 'server.js' DEBUG: --watch '.' DEBUG: --extensions 'node|js' DEBUG: --exec 'node' DEBUG: Starting child process with 'node server.js' DEBUG: Watching directory '/home/Owner/sites/Node-JS-todo-list-tutorial/.' for c hanges. Starting server @ http://127.0.0.1:1337/ Server running at http://127.0.0.1:1337/
Now if I make a change in server.js or any included modules:
DEBUG: crashing child DEBUG: Starting child process with 'node server.js' Starting server @ http://127.0.0.1:1337/ Server running at http://127.0.0.1:1337/
If the saved changes have errors, supervisor will output the errors, but keep trying to restart the server.
DEBUG:
node.js:134
throw e; // process.nextTick error, or 'error' event on first tick
DEBUG: ^
DEBUG: ReferenceError: zxc is not defined
at Object.<anonymous> (/home/Owner/sites/Node-JS-todo-list-tutorial/server.j
s:10:1)
at Module._compile (module.js:407:26)
at Object..js (module.js:413:10)
at Module.load (module.js:339:31)
at Function._load (module.js:298:12)
at Array.<anonymous> (module.js:426:10)
at EventEmitter._tickCallback (node.js:126:26)
DEBUG: Starting child process with 'node server.js'
Starting server @ http://127.0.0.1:1337/
Supervisor seems like a great module to keep your Node App running fresh. There are also a few command line options for supervisor that might come in handy that I may explore in a future post. For now I recommend using supervisor at least in a development environment to save you fingers from a few extra CTRL-C stretches.
-
Николай Кучумов
-
http://twitter.com/nedudi Dmitry Dudin
Projects & Profiles
Categories
- Blogging
- Caching
- CakePHP
- Career
- Clojure
- Concurrent Programming
- ContractLib
- Database Testing
- Design by contract
- Design Patterns
- Front Controller
- Git
- Handlebars.js
- HTML5
- iOS / iPhone / iPad Development
- Issue/Bug Tracking
- Javascript
- jQuery
- jQuery easyBars
- jQuery Plugins
- JSON
- Lithium PHP
- MVC
- MySQL
- node.js
- NoSQL
- Phactory
- PHP
- PHP Frameworks
- PHPUnit
- Podcasts
- Responsive Web Design
- Scala
- Sessions
- Slides
- SQL
- Testing
- Unit Testing
- Version Control
- Web Development / Programming
- Web Services
Tags
async beanstalk beanstalkapp blogging bug tracker bug tracking cakephp cakephp google group clojure git github giving a technical presentation handlebars handlebarsjs ios web development issue tracker issue tracking javascript jquery lighthouse lithium lithium filters lithium php mobile safari mobile web mvc mysql mysysgit natural sort natural sorting netbeans node node.js php php 5.3 phpunit podcasts self executing anonymous functions sql subversion svn ticket bins tickets versioning wordpress




