如何让服务器监听在Grunt任务中运行?

时间:2015-01-30 16:32:18

标签: javascript node.js asynchronous gruntjs

我有一台HTTP服务器,我作为一个Grunt任务的一部分运行。 listen方法是异步的(就像大多数Node.js代码一样),所以在Grunt任务调用方法之后,它立即完成执行,从而关闭服务器。

grunt.registerTask('serveProxy', 'Start the proxy to the various servers', function() {
    var server = http.createServer(function(req, res) {
        // ...
    });
    server.listen(80);
});

我如何保持此运行或者可能使方法阻止以便它不会返回?

1 个答案:

答案 0 :(得分:3)

解决方案是指示Grunt等待as per the documentation告诉Grunt这是一个异步方法并使用回调来指示我们何时完成。

grunt.registerTask('serveProxy', 'Start the proxy to the various servers', function() {
    var done = this.async();
    var server = http.createServer(function(req, res) {
        // ...
    });
    server.listen(80);
});