Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting grunt-express to restart on changes

I'm trying to use grunt-express and grunt-watch. I would like the server to reload with I change my server file.

Here is what I got.

Gruntfile.js

var path = require('path');
module.exports = function(grunt) {
  grunt.initConfig({
    express: {
      options: {
        port: 8000
      },
      load: {
        server: path.resolve('./app')
      }
    },
    watch: {
      express: {
        files: ['app.js'],
        tasks: ['express:load']

      }
    }
  });
  grunt.loadNpmTasks('grunt-karma');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-express');

  grunt.registerTask('server', ['express:load', 'express-keepalive', 'watch']);
};

app.js

var express = require('express');
var app = express();
var server = require('http').createServer(app);

app.get('/', function(req, res) {
  res.sendfile(__dirname + '/public/index.html');
});
exports = module.exports = server;
exports.use = function() {
  app.use.apply(app, arguments);
};

When I type grunt server in the console I get:

Running "express:load" (express) task

Running "express-server:load" (express-server) task
Web server started on port:8000, no hostname specified [pid: 21115]

Running "express-keepalive" task

The server start up fine and I can go to localhost:8000 to view my page. The watch task doesn't seem to start and when I make changes to app.js it doesn't restart. I basically want type grunt server and then when any changes happen to app.js, I want the server to restart. I've tried using the serverreload option, but I can't seem to get that to work either. I have also tried using express-restart (in place of express:livereload) in the watch:express task, but it says

Warning: path must be a string Use --force to continue.
like image 624
JoelV Avatar asked Feb 26 '14 03:02

JoelV


1 Answers

After some playing around I was able to get it working, with an annoying issue. Here's the core of the Gruntfile:

var path = require('path');

module.exports = function (grunt) {
  grunt.initConfig({
    express: {
      options: {
        port: 8000,
        hostname: '*'
      },
      livereload: {
        options: {
          server: path.resolve('./app.js'),
          livereload: true,
          serverreload: true,
          bases: [path.resolve('./public')]
        }
      }
    }
  });

  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-express');

  grunt.registerTask('default', ['express', 'express-keepalive']);
};

However there seems to be an issue where a different port is used every other save. If the other server reload option is working I would stick with that for the time being.

As a side note, it appears this grunt plugin also listening on the port which means the start app.js should return itself with module.exports = app; rather than the default express http.createServer. It appears both will run simultaneously as long as different ports are specified.

like image 68
pk-nb Avatar answered Oct 04 '22 22:10

pk-nb