运行带有gulp的命令启动Node.js服务器 [英] Running a command with gulp to start Node.js server

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

问题描述

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

  var exec = require('child_process')。 

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

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

  .pipe 

然而,我是新来的gulp,我认为这是你如何通过命令/任务。谢谢,谢谢。

解决方案

  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。 p>

编辑



由于下面提到的@ N1mr0d,没有服务器输出更好的方法运行你的服务器将使用nodemon,你可以简单地运行 nodemon server.js ,就像运行 node server.js



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

  //启动我们的服务器并监听更改
gulp.task('server',function(){
// configure nodemon
nodemon({
//脚本运行app
脚本:'server.js',
//这个监听任何这些文件中的更改/ route并重新启动应用程序
watch:[server.js,app.js,routes /,'public / *','public / * / **'],
ext:'js'
//下面我使用es6箭头函数,但你可以删除箭头,并使其成为正常的.on('restart',function(){//然后把你的东西放在这里}
})。on('restart',()=> {
gulp.src('server.js')
//我添加了notify,它在重新启动时显示一条消息。更多的是我测试,所以你可以删除这个
.pipe(通知(运行开始的任务和东西));
});
});

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


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

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

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

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.

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

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

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

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

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