Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run node server using gulp task

Tags:

node.js

gulp

I am new to gulp.

I need to run node server using gulp but without using any of its plugin.

Is it possible?

If not then what is the best plugin for it in gulp system.

like image 972
Mozak Avatar asked Jul 06 '26 12:07

Mozak


2 Answers

You should try gulp-nodemon plugin. It's useful plugin for development with node.

// Gulpfile.js
var gulp = require('gulp')
  , nodemon = require('gulp-nodemon')
  , jshint = require('gulp-jshint')

gulp.task('lint', function () {
  gulp.src('./**/*.js')
    .pipe(jshint())
})

gulp.task('develop', function () {
  nodemon({ script: 'server.js'
          , ext: 'html js'
          , ignore: ['ignored.js']
          , tasks: ['lint'] })
    .on('restart', function () {
      console.log('restarted!')
    })
})

For more details you can visit here.

Also

If you don't want use any plugin, you can start node without it like below:

var gulp = require('gulp')
  , exec = require('child_process').exec

gulp.task('nodestart', function (cb) {
  exec('node bin/www.js', function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
  });
})
like image 141
Furkan Başaran Avatar answered Jul 11 '26 07:07

Furkan Başaran


There are several plugins available with gulp.

  1. Nodemon (https://www.npmjs.com/package/gulp-nodemon)
  2. Express (https://www.npmjs.com/package/gulp-express)
  3. Develop Server (https://www.npmjs.com/package/gulp-develop-server)

if you wish to start the node server without any plugin you can try out the following.

var exec = require('child_process').exec;

gulp.task('start', function (callback) {
    exec('node server/app.js', function (err, stdout, stderr) {
        console.log(stdout);
        console.log(stderr);
        callback(err);
    });
});
like image 29
Abhay Pai Avatar answered Jul 11 '26 08:07

Abhay Pai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!