【问题标题】:Running node app from grunt with watch使用手表从 grunt 运行节点应用程序
【发布时间】:2012-12-03 01:34:57
【问题描述】:

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

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' );
};

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

【问题讨论】:

    标签: node.js gruntjs


    【解决方案1】:

    您可以设置 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)
    });
    

    然后照常运行node app

    Credit

    【讨论】:

    • 您好,您有该参考资料的更新链接吗?我试图找到它,但没有运气。另外,我们可以不用创建子进程,而是直接使用 grunt-exec 通过 supervisor 启动应用程序吗?
    • 看起来该博客现在已关闭:/ 我没有任何其他参考。至于使用supervisor,应该是可以的,不过我不太熟悉。
    • 谢谢。关于您的示例,为什么不从 Grunt 运行您的应用程序,而不是反过来呢?虽然我猜这样你可以使用像 nodemon 这样的东西,只要有变化就会重新启动你的服务器,然后再次运行 grunt。
    • 现在可以根据answer 从 Grunt 运行应用程序。另外,我找到了archive of the reference
    猜你喜欢
    • 1970-01-01
    • 2014-01-19
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    • 1970-01-01
    • 1970-01-01
    • 2013-02-09
    • 1970-01-01
    相关资源
    最近更新 更多