user1817849
user1817849

Reputation: 131

grunt server can't be connected <gruntjs>

module.exports = function(grunt) {

  // Project configuration.
    grunt.initConfig({
      server: {
        port: 8888,
        base: '.'
      }
    });

};

C:\Program Files\nodejs\test\grunt>
C:\Program Files\nodejs\test\grunt>grunt server
Running "server" task
Starting static web server on port 8888.

Done, without errors.

but can't connected by input [http://127.0.0.1:8888][1] in browsers ! jiong~

How about to fix this problem in windows or unix ?

Upvotes: 13

Views: 10641

Answers (4)

adardesign
adardesign

Reputation: 35691

By default Grunt starts up the server just for testing (or any other task asked..) and as soon as it's done it exits....

But fortunately I found a solution which by adding this to your grunt.js file will let you (optionally) halt the server from exiting.

grunt.registerTask('wait', 'Wait for a set amount of time.', function(delay) {
   var d = delay ? delay + ' second' + (delay === '1' ? '' : 's') : 'forever';
   grunt.log.write('Waiting ' + d + '...');
   // Make this task asynchronous. Grunt will not continue processing
   // subsequent tasks until done() is called.
   var done = this.async();
  // If a delay was specified, call done() after that many seconds.
   if (delay) { setTimeout(done, delay * 1000); }
});

Then in your command line call it: grunt server wait then you should be able to see it in the browser..

Make sure you add it inside module.exports = function(grunt){...}

Upvotes: 0

David Souther
David Souther

Reputation: 8174

Don't use grunt to serve your project. Grunt is a build tool. Instead, use npm lifecycle scripts.

server.js

var express = require("express"),
    app = express();
app.use('/', express.static(__dirname));
app.listen(8888);

package.json

{
    "name": "my-project",
    "scripts": {
        "start": "node server.js"
    },
    "dependencies": {
        "express": "3"
    }
}

Now you can run npm start and life will be great. Grunt is a build tool, not a server. npm is a package lifecycle manager, not a build tool. Express is a server library. Use each in its right place.

Follow up (2013-08-15)

The exception to this rule is when you're needing to serve your project to other testing tools in your build stack. The grunt-contrib-connect plugin is designed specifically with this use case in mind, and has a keepalive configuration setting that will leave grunt open while serving your static files. This is usually used in conjunction with a watch task that runs a test suite when either the tests or the code changes.

Upvotes: 5

Sindre Sorhus
Sindre Sorhus

Reputation: 63477

In grunt 0.4 combined with grunt-contrib-connect you can run a long running server by using the keepalive argument: grunt connect:target:keepalive or define it as an option in your config:

grunt.initConfig({
  connect: {
        target:{
            options: {
                port: 9001,
                keepalive: true
            }
        }
    }
});

Upvotes: 26

Chris Calo
Chris Calo

Reputation: 7828

The server task only runs as long as it is needed, but you can keep it from quitting. From a comment by widget on another question: In your grunt.js file define a task named run that runs the tasks server and watch.

grunt.registerTask("run", "server watch");

The watch task runs indefinitely, so it prevents the server task from ending. Just make sure you also have a config for the watch task. Here it is all together in your grunt.js file:

module.exports = function (grunt) {
  // …
  grunt.initConfig({
    // …
    watch: {
      files: "<config:lint.files>",
      tasks: "lint qunit",
    },
    // …
  });

  grunt.registerTask("run", "server watch");
};

From the command line just type:

$ grunt run

The server will stay up and running.

Alternatively, as @NateBarr points out, from the command line you can run:

$ grunt server watch

Upvotes: 4

Related Questions