使用 gulp 运行命令以启动 Node.js 服务器 [英] Running a command with gulp to start Node.js server

查看:112
本文介绍了使用 gulp 运行命令以启动 Node.js 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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.这就是我想要完成的.在我的终端窗口中,它抱怨我的

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

.pipe

但是,我是 gulp 的新手,我认为这就是您传递命令/任务的方式.感谢您提供任何帮助,谢谢.

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

推荐答案

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

我认为这段代码:

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

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

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.

编辑

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

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.js like you would run node server.js.

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

The below code snippet is what I use in my gulp task to run my server now using 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

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

这篇关于使用 gulp 运行命令以启动 Node.js 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆