栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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。您可以

nodemonserver.js
像奔跑一样简单地奔跑
node server.js

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

// start our server and listen for changesgulp.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**'],        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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/427102.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号