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

How to keep server listen running in Grunt task?

我有一个 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);
});

我怎样才能保留这个 运行 或使方法阻塞而不是 return?

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

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