我有两个服务器脚本(两者都依赖于socket.io;在不同的端口上运行)。
我想通过gulp并行开始。但另外我想有可能阻止其中一个。甚至可以访问每个脚本的控制台输出。
现有解决方案吗?或者你甚至建议使用除了gulp以外的任何东西吗?
答案 0 :(得分:4)
我找到了一个解决方案,我另外启动了一个mongoDB服务器:
var child_process = require('child_process');
var nodemon = require('gulp-nodemon');
var processes = {server1: null, server2: null, mongo: null};
gulp.task('start:server', function (cb) {
// The magic happens here ...
processes.server1 = nodemon({
script: "server1.js",
ext: "js"
});
// ... and here
processes.server2 = nodemon({
script: "server2.js",
ext: "js"
});
cb(); // For parallel execution accept a callback.
// For further info see "Async task support" section here:
// https://github.com/gulpjs/gulp/blob/master/docs/API.md
});
gulp.task('start:mongo', function (cb) {
processes.mongo = child_process.exec('mongod', function (err, stdout, stderr) {});
cb();
});
process.on('exit', function () {
// In case the gulp process is closed (e.g. by pressing [CTRL + C]) stop both processes
processes.server1.kill();
processes.server2.kill();
processes.mongo.kill();
});
gulp.task('run', ['start:mongo', 'start:server']);
gulp.task('default', ['run']);
答案 1 :(得分:0)