【问题标题】:Running a command with gulp to start Node.js server使用 gulp 运行命令以启动 Node.js 服务器
【发布时间】:2015-03-18 20:09:16
【问题描述】:

所以我正在使用 gulp-exec (https://www.npmjs.com/package/gulp-exec),它在阅读了一些文档后提到,如果我只想运行一个命令,我不应该使用插件并使用我尝试过的代码在下面使用。

var    exec = require('child_process').exec;

gulp.task('server', function (cb) {
  exec('start server', function (err, stdout, stderr) {
    .pipe(stdin(['node lib/app.js', 'mongod --dbpath ./data']))
    console.log(stdout);
    console.log(stderr);
    cb(err);
  });
})

我正在尝试让 gulp 启动我的 Node.js 服务器和 MongoDB。这就是我想要完成的。在我的终端窗口中,它抱怨我的

.pipe

但是,我是 gulp 的新手,我认为这就是您通过命令/任务的方式。任何帮助表示赞赏,谢谢。

【问题讨论】:

标签: javascript node.js express gulp


【解决方案1】:
gulp.task('server', function (cb) {
  exec('node lib/app.js', function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
  });
  exec('mongod --dbpath ./data', function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
  });
})

供将来参考,如果其他人遇到此问题。

上面的代码解决了我的问题。所以基本上,我发现上面是它自己的功能,因此不需要:

.pipe

我以为这段代码:

exec('start server', function (err, stdout, stderr) {

是我正在运行的任务的名称,但它实际上是我将要运行的命令。因此,我将其更改为指向运行我的服务器的 app.js 并指向我的 MongoDB。

编辑

正如下面提到的@N1mr0d,没有服务器输出,运行服务器的更好方法是使用 nodemon。您可以像运行node server.js 一样简单地运行nodemon server.js

下面的代码 sn-p 是我在 gulp 任务中使用的,现在使用 nodemon 运行我的服务器:

// start our server and listen for changes
gulp.task('server', function() {
    // configure nodemon
    nodemon({
        // the script to run the app
        script: 'server.js',
        // this listens to changes in any of these files/routes and restarts the application
        watch: ["server.js", "app.js", "routes/", 'public/*', 'public/*/**'],
        ext: 'js'
        // Below i'm using es6 arrow functions but you can remove the arrow and have it a normal .on('restart', function() { // then place your stuff in here }
    }).on('restart', () => {
    gulp.src('server.js')
      // I've added notify, which displays a message on restart. Was more for me to test so you can remove this
      .pipe(notify('Running the start tasks and stuff'));
  });
});

安装 Nodemon 的链接:https://www.npmjs.com/package/gulp-nodemon

【讨论】:

  • 如何停止其中一台服务器?
  • @tmuecksch 上面我在 gulp 任务中运行我的节点服务器和数据库。如果你想停止一个并运行一个,你必须有两个不同的 gulp 任务
  • @tmuecksch exec('killall mongod', function (err, stdout, stderr) {
  • 我发现 npmjs.com/package/gulp-nodemon 是一个非常好的解决方案,同时还具有将 nodemon 集成到其中的额外好处。
  • @T.Webster 我从来不知道!虽然我确实喜欢 gulp-nodemon 的简单性以及它为您提供的东西。但我会考虑使用child_process.spawn() 方法谢谢!
【解决方案2】:

此解决方案在出现时显示 stdout/stderr,并且不使用第 3 方库:

var spawn = require('child_process').spawn;

gulp.task('serve', function() {
  spawn('node', ['lib/app.js'], { stdio: 'inherit' });
});

【讨论】:

  • 只记得在重启之前杀死子进程。
【解决方案3】:

您也可以像这样创建 gulp 节点服务器任务运行器:

gulp.task('server', (cb) => {
    exec('node server.js', err => err);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-30
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    • 2014-10-18
    • 2017-04-09
    • 2019-08-15
    相关资源
    最近更新 更多