13
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 ?

Manse
  • 37,765
  • 10
  • 83
  • 108
user1817849
  • 131
  • 1
  • 1
  • 4

4 Answers4

26

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
            }
        }
    }
});
Sindre Sorhus
  • 62,972
  • 39
  • 168
  • 232
5

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.

David Souther
  • 8,125
  • 2
  • 36
  • 53
  • 2
    I know this is old, but if you use express.static('.') sub-directories (such as css, js, etc) will get a 403 error. Use express.static(__dirname) instead. – Nick Mitchinson Mar 23 '13 at 20:19
  • Absolutely correct for best practices. Edited answer to use __dirname. – David Souther Mar 24 '13 at 19:44
  • Grunt isn't necessarily limited to being a "build tool". Grunt is a task runner (self proclaimed "The JavaScript Task Runner" on its website) similar to Rake in Ruby, it just so happens that it is extremely useful for running project builds. The options for the https://github.com/gruntjs/grunt-contrib-connect task even have a `keepalive` option that is intended for running a persistent connect server as a grunt task. – Tres Aug 15 '13 at 00:54
  • Tres, of course you can run arbitrary tasks with Grunt. Best practice IMHO is not to. The connect task's keepalive is designed to work with watch and running local CI servers, not to be a management tool for production. That's a tool NPM provides, and provides very well. – David Souther Aug 15 '13 at 00:58
  • David, I totally agree that it shouldn't be a production tool. However, I think it's misleading to say "don't ever run arbitrary tasks" because that's exactly what it's designed for. For example, I may want to run my Connect task that serves my Mocha tests so that I don't have to duplicate any functionality to run them in the browser instead of having them run via Grunt as I develop a feature. I don't disagree with you, I just don't believe it's as black and white as you claim. – Tres Aug 15 '13 at 01:08
  • 1
    Pedagogically, in the context of this question, I felt it more useful to present a black and white answer. Perhaps the original Q was indeed in the context of testing - the most common and incredibly useful reason to run grunt as a server - but I didn't get that from the Q, and find it more appropriate to take away options, until the questioner is more comfortable with the tools. – David Souther Aug 15 '13 at 13:39
  • 1
    +1 for the use of pedagogy :), clarification and erring on the side of caution. – Tres Aug 16 '13 at 02:28
4

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
Community
  • 1
  • 1
Chris Calo
  • 7,518
  • 7
  • 48
  • 64
0

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){...}

shim
  • 9,289
  • 12
  • 69
  • 108
adardesign
  • 33,973
  • 15
  • 62
  • 84
  • who downvoted this? "Grunt starts up the server just for testing (or any other task asked..) and as soon as its done it exits...." this is a great information. – allenhwkim Dec 29 '13 at 04:11