Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a command with gulp to start Node.js server

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.

like image 370
pourmesomecode Avatar asked Jan 20 '15 14:01

pourmesomecode


People also ask

What command has to be used to start the node js server?

To use http-server , install it with the command npm install http-server -g . Visit http://localhost:8081 to verify that the server is running and serves our file with the "Hello World" message. This Node. js serving option is useful for serving a simple app that does mainly front-end work.


2 Answers

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

like image 92
pourmesomecode Avatar answered Sep 19 '22 16:09

pourmesomecode


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

var spawn = require('child_process').spawn;  gulp.task('serve', function() {   spawn('node', ['lib/app.js'], { stdio: 'inherit' }); }); 
like image 31
pein-consulting.de Avatar answered Sep 16 '22 16:09

pein-consulting.de