Javascript 使用 gulp 运行命令以启动 Node.js 服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28048029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:08:51  来源:igfitidea点击:

Running a command with gulp to start Node.js server

javascriptnode.jsexpressgulp

提问by pourmesomecode

So I am using gulp-exec (https://www.npmjs.com/package/gulp-exec) which after reading some of the documentation it mentions that if I want to just run a command I shouldn't use the plugin and make use of the code i've tried using below.

所以我正在使用 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);
  });
})

I'm trying to get gulp to start my Node.js server and MongoDB. This is what i'm trying to accomplish. In my terminal window, its complaining about my

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

.pipe

However, I'm new to gulp and I thought that is how you pass through commands/tasks. Any help is appreciated, thank you.

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

回答by pourmesomecode

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);
  });
})

For future reference and if anyone else comes across this problem.

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

The above code fixed my problem. So basically, I found out that the above is its own function and therefore, doesn't need to:

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

.pipe

I thought that this code:

我认为这段代码:

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

was the name of the task I am running however, it is actually what command I will be running. Therefore, I changed this to point to app.js which runs my server and did the same to point to my MongoDB.

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

EDIT

编辑

As @N1mr0d mentioned below with having no server output a better method to run your server would be to use nodemon. You can simply run nodemon server.jslike you would run node server.js.

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

The below code snippet is what I use in my gulp task to run my server now using nodemon :

下面的代码片段是我在 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'));
  });
});

Link to install Nodemon : https://www.npmjs.com/package/gulp-nodemon

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

回答by pein-consulting.de

This solution has stdout/stderr shown as they occur and does not use 3rd party libs:

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

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

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

回答by radhey shyam

You can also create gulp node server task runner like this:

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

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