grunt server can't be connected <gruntjs>

自古美人都是妖i 提交于 2019-12-20 18:29:53

问题


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 ?


回答1:


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
            }
        }
    }
});



回答2:


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.




回答3:


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



回答4:


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



来源:https://stackoverflow.com/questions/13342606/grunt-server-cant-be-connected-gruntjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!