简体   繁体   中英

How to keep server listen running in Grunt task?

I have a HTTP server I'm running as part of one Grunt task. 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.

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.registerTask('serveProxy', 'Start the proxy to the various servers', function() {
    var done = this.async();
    var server = http.createServer(function(req, res) {
        // ...
    });
    server.listen(80);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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