Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

automatically reloading Koa server

I've started playing around with Koa, but so far have been unable to find a decent solution for automatically reloading the application upon code changes.

My understanding is that nodemon is the preferred solution in the Node universe, but I'm getting errors due to the --harmony flag (required by Koa):

$ node_modules/.bin/nodemon /path/to/node-unstable/bin/node app.js
/path/to/node-unstable/bin/node --harmony $@
                     ^^^^^^^
SyntaxError: Unexpected identifier
[nodemon] app crashed - waiting for file changes before starting...
like image 301
AnC Avatar asked Feb 27 '14 07:02

AnC


3 Answers

node_modules/.bin/nodemon --harmony-generators app.js should be sufficient

like image 155
Jonathan Ong Avatar answered Nov 04 '22 06:11

Jonathan Ong


I would like to recommend you "pm2" : http://pm2.keymetrics.io/

pm2 is a process manager. It manages your applications states, so you can start, stop, restart and delete processes.

You can easily install pm2 (generally on your machine) typing: sudo npm install -g pm2

Basically pm2 when see some changes in your code, he restart your process istantly, if your process crash he will be restarted and the error will be logged.. For more information take a look on the documentation page: http://pm2.keymetrics.io/docs/usage/cluster-mode/

like image 38
davidev Avatar answered Nov 04 '22 05:11

davidev


Setting the 'execMap' option in this gulp task works for me:

var nodemon = require('gulp-nodemon');

gulp.task('serve-dev', function() {
    var options = {
        script: './src/server/app.js',
        execMap: { 
            "js": "node --harmony"
        },
        delayTime: 1,
        env: {
            'PORT': port,
            'NODE_ENV': 'dev'
        },
        watch: ['./src/server/']
    };

    return nodemon(options);
});

Obviously your other options may differ, but I included the whole thing as it annoys me when I'm learning something to only see the bare minimum in an answer.

like image 41
Facio Ratio Avatar answered Nov 04 '22 05:11

Facio Ratio