automatically reloading Koa server
Asked Answered
E

5

9

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...
Embowel answered 27/2, 2014 at 7:52 Comment(2)
How about this? $ node_modules/.bin/nodemon -x "/path/to/node-unstable/bin/node --harmony" app.jsRobson
Thanks, that worked - clearly I should have RTFM more carefully. (Though I initially had some issues with the address being in use, so I wrote a wrapper script to ensure the existing server is shut down first.)Embowel
A
15

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

Apologue answered 27/2, 2014 at 9:4 Comment(3)
That's what I'd assumed as well, but that gives me the same syntax error.Embowel
maybe you're not even using node v0.11Apologue
You're right, the v0.11 version (intentionally) wasn't on my PATH, so that couldn't have worked - works fine with export PATH="node-unstable/bin:$PATH".Embowel
V
3

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/

Videogenic answered 8/12, 2016 at 10:19 Comment(0)
H
1

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.

Herdsman answered 8/7, 2015 at 18:28 Comment(0)
C
0

I've recently finished creating a simple web API using KOA, and as for a reload action after code changes i used babel-watch

The advantage of using it is already stated in the repo:

If you're tired of using babel-node together with nodemon (or similar solution). The reason why the aforementioned setup performs so badly is the startup time of babel-node itself. babel-watch only starts babel in the "master" process where it also starts the file watcher. The transpilation is performed in that process too. On file-watcher events, it spawns a pure node process and passes transpiled code from the parent process together with the source maps. This allows us to avoid loading babel and all its deps every time we restart the JS script/app.

Checklist answered 17/4, 2017 at 10:41 Comment(0)
T
0

My Koa server is working fine with

nodemon server.js
Thorner answered 2/4, 2021 at 10:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.