PourMeSomeCode提出了一个问题:Running a command with gulp to start Node.js server,或许与您遇到的问题类似。
回答者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.
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
希望本文对你有帮助,欢迎支持JavaScript中文网