使用手表从grunt运行节点应用程序

ahe*_*ann 3 node.js gruntjs

所以,我有下面的grunt文件.我想添加一个任务,它将启动我的节点应用程序并监视目录中的更改并重新启动.我一直在使用supervisor,node-dev(这很棒)但我想运行一个命令并启动我的整个应用程序.必须有一个简单的方法来做到这一点,但我只是想念它.它也用coffeescript编写(不确定是否会改变)...

module.exports = function(grunt) {
  grunt.initConfig({
    /*exec: {
        startApi: {
            command: "npm run-script start-api"
        }
    },*/
    //static server
    server: {
        port: 3333,
        base: './public',
        keepalive: true
    },

    // Coffee to JS compilation
    coffee: {
        compile: {
            files: {
                './public/js/*.js': './src/client/app/**/*.coffee'
            },
            options: {
                //basePath: 'app/scripts'
            }
        }
    },


    mochaTest: {
        all: ['test/**/*.*']
    },


    watch: {
        coffee: {
            files: './src/client/app/**/*.coffee',
            tasks: 'coffee'
        },
        mochaTest: {
            files: 'test/**/*.*',
            tasks: 'mochaTest'
        }
    }
});

grunt.loadNpmTasks('grunt-contrib-coffee');
grunt.loadNpmTasks('grunt-mocha-test');
//grunt.loadNpmTasks('grunt-exec');

grunt.registerTask( 'default', 'server coffee mochaTest watch' );
};
Run Code Online (Sandbox Code Playgroud)

正如您在注释中看到的,我尝试了grunt-exec,但node命令会停止执行其他任务.

fly*_*ish 8

您可以设置grunt以在启动节点应用程序时运行默认任务和监视任务:

在app.js

var cp = require('child_process');
var grunt = cp.spawn('grunt', ['--force', 'default', 'watch'])

grunt.stdout.on('data', function(data) {
    // relay output to console
    console.log("%s", data)
});
Run Code Online (Sandbox Code Playgroud)

然后node app正常运行!

信用

  • 现在可以根据[答案](http://stackoverflow.com/questions/15044026/running-node-app-through-grunt)从Grunt运行应用程序.另外,我找到了[参考档案](http://web-archive-me.com/page/1254392/2013-01-29/http://cobbweb.me/blog/2012/06/07/使用-咕噜点-JS-到集结你的-前端-IN-A-节点点-JS-斜线快递点-JS-应用程序/) (6认同)