无法停止由nodemon运行的服务器
问题描述:
我为单元测试创建了gulp任务。我添加nodemon以自动运行服务器,然后运行测试。但再次运行gulp任务时出错。我有错误,说明该端口已经忙于另一个进程。无法停止由nodemon运行的服务器
我的用户验证码:
var gulp = require('gulp'),
gulpUtil = require('gulp-util'),
gulpShell = require('gulp-shell'),
gulpEnv = require('gulp-env'),
gulpNodemon = require('gulp-nodemon'),
gulpMocha = require('gulp-mocha');
gulp.task('default', function() {
gulpUtil.log('unit - run unit tests');
});
gulp.task('server', function (callback) {
var started = false;
return gulpNodemon({
script: './build/app.js'
})
.on('start', function() {
if (!started) {
started = true;
return callback();
}
})
});
gulp.task('unit', ['server'], function() {
return gulp.src('./src/*.js')
.pipe(gulpMocha({reporter: 'spec'}))
.once('error', function() {
process.exit(1);
})
.once('end', function() {
process.exit();
})
});
如何停止或之后的单元测试杀服务器?
加成回答: 现在我有gulpfile.js:
var gulp = require('gulp'),
gulpUtil = require('gulp-util'),
gulpNodemon = require('gulp-nodemon'),
gulpMocha = require('gulp-mocha'),
gulpShell = require('gulp-shell');
var runSequence = require('run-sequence');
var nodemon;
gulp.task('default', function() {
gulpUtil.log('compile - compile server project');
gulpUtil.log('unit - run unit tests');
});
gulp.task('compile', function() {
return gulp.src('./app/main.ts')
.pipe(gulpShell([
'webpack'
]))
});
gulp.task('server', function (callback) {
nodemon = gulpNodemon({
script: './build/app.js'
})
.on('start', function() {
return callback();
})
.on('quit', function() {
})
.on('exit', function() {
process.exit();
});
return nodemon;
});
gulp.task('test', function() {
return gulp.src('./src/*.js')
.pipe(gulpMocha({reporter: 'spec'}))
.once('error', function() {
nodemon.emit('quit');
})
.once('end', function() {
nodemon.emit('quit');
});
});
gulp.task('unit', function() {
runSequence('compile', 'server', 'test');
});
而且在我的服务器脚本我加入这个片段:
this.appListener = this.http.listen(process.env.PORT || 3000, '0.0.0.0', function() {
console.log(chalk.green("Server started with port " + _this.appListener.address().port));
});
// **Add**
function stopServer() {
console.log(chalk.cyan('Stop server'));
process.exit();
}
process.on('exit', stopServer.bind(this));
process.on('SIGINT', stopServer.bind(this));
因此,测试时,完成后我在服务器脚本中调用process.exit()
我添加事件退出事件处理程序停止服务器和吞吐任务已成功完成停止的服务器。
答
Nodemon有一个quit
命令。看看Using nodemon events和你的模块也docs。根据该文件,你可以使用:
var nodemon = require('nodemon');
// force a quit
nodemon.emit('quit');
这里看看http://stackoverflow.com/questions/32953294/cannot-stop-gulp-with-ctrlc-when-using-一饮而尽,nodemon-一饮而尽手表,一起 –