简体   繁体   English

如何在Grunt任务中保持服务器侦听运行?

[英]How to keep server listen running in Grunt task?

I have a HTTP server I'm running as part of one Grunt task. 我有一个HTTP服务器,这是Grunt任务的一部分。 The listen method is asynchronous (as is most Node.js code), so immediately after the Grunt task has called the method it finishes execution and thus closes the server. 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);
});

How can I keep this running or perhaps make the method block so it doesn't return? 我如何才能保持运行状态或使方法块不返回?

The solution was to instruct Grunt to wait as per the documentation by telling Grunt this is an asynchronous method and using a callback to indicate when we are done. 解决方案是通过告诉Grunt这是一个异步方法并使用回调指示完成的时间,以指示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);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM