无法停止由 nodemon 运行的服务器

Met*_*dio 1 node.js nodemon gulp

我为单元测试创​​建了 gulp 任务。我添加 nodemon 以自动运行服务器然后运行测试。但是再次运行 gulp 任务时出现错误。我有错误,该端口已经忙于另一个进程。

我使用此代码:

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();
        })
});
Run Code Online (Sandbox Code Playgroud)

单元测试后如何停止或终止服务器?

除了回答: 现在我有 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');
});
Run Code Online (Sandbox Code Playgroud)

同样在我的服务器脚本中,我添加了这个片段:

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));
Run Code Online (Sandbox Code Playgroud)

因此,当测试完成并调用process.exit()服务器脚本时,我添加了停止服务器和吞咽任务成功完成停止服务器的事件退出事件处理程序。

ju_*_*ju_ 6

Nodemon 有一个quit命令。看看使用 nodemon 事件和关于你的模块还有它的文档。根据您可以使用的文档:

var nodemon = require('nodemon');

// force a quit
nodemon.emit('quit');
Run Code Online (Sandbox Code Playgroud)