admin管理员组

文章数量:1323723

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.

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.
Share Improve this question edited Jul 13, 2017 at 19:12 Daniel Nugent 43.3k15 gold badges113 silver badges140 bronze badges asked Feb 26, 2014 at 3:24 JoelVJoelV 851 silver badge8 bronze badges 3
  • What grunt-express version have you specified in your package.json file? If it's v1.0, looks like the express task takes all the config and the watch task is unnecessary. – pk-nb Commented Feb 26, 2014 at 4:22
  • I'm using 1.2.1. So now my question is how do I set up my Gruntfile to run reload the express server when there are changes to app.js. – JoelV Commented Feb 26, 2014 at 5:54
  • I've found another grunt plug-in called grunt-express-server that I was able to set up, and get working. However I'm still wondering how to get that serverreload option working correctly in grunt-express plugin. – JoelV Commented Feb 26, 2014 at 13:52
Add a ment  | 

1 Answer 1

Reset to default 7

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.

本文标签: javascriptGetting gruntexpress to restart on changesStack Overflow