简体   繁体   English

grunt服务器无法连接<gruntjs>

[英]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~ 但是[http://127.0.0.1:8888][1] in browsers ! jiong~无法通过输入[http://127.0.0.1:8888][1] in browsers ! jiong~连接[http://127.0.0.1:8888][1] in browsers ! jiong~ [http://127.0.0.1:8888][1] in browsers ! jiong~

How about to fix this problem in windows or unix ? 如何在windows或unix中修复此问题?

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 0.4中结合grunt-contrib-connect,您可以使用keepalive参数运行长时间运行的服务器: grunt connect:target:keepalive或将其定义为配置中的选项:

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

Don't use grunt to serve your project. 不要使用grunt来为您的项目服务。 Grunt is a build tool. Grunt是一个构建工具。 Instead, use npm lifecycle scripts. 相反,使用npm生命周期脚本。

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. 现在你可以运行npm start ,生活会很棒。 Grunt is a build tool, not a server. Grunt是一个构建工具,而不是服务器。 npm is a package lifecycle manager, not a build tool. npm是包生命周期管理器,而不是构建工具。 Express is a server library. Express是一个服务器库。 Use each in its right place. 在正确的位置使用每个。

Follow up (2013-08-15) 跟进(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. grunt-contrib-connect插件专门针对这个用例而设计,并具有keepalive配置设置,在为静态文件提供服务时会打开grunt。 This is usually used in conjunction with a watch task that runs a test suite when either the tests or the code changes. 这通常与在测试或代码更改时运行测试套件的watch任务结合使用。

The server task only runs as long as it is needed, but you can keep it from quitting. server任务只在需要时运行,但您可以防止它退出。 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.js文件中定义一个名为run的任务,该任务运行任务serverwatch

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

The watch task runs indefinitely, so it prevents the server task from ending. watch任务无限期运行,因此可以防止server任务结束。 Just make sure you also have a config for the watch task. 只需确保您还有watch任务的配置。 Here it is all together in your grunt.js file: 这里grunt.js都在你的grunt.js文件中:

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: 或者,正如@NateBarr指出的那样,您可以从命令行运行:

$ grunt server watch

By default Grunt starts up the server just for testing (or any other task asked..) and as soon as it's done it exits.... 默认情况下,Grunt启动服务器只是为了测试(或任何其他要求的任务..),一旦它完成它退出....

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.js文件中,您可以(可选)暂停服务器退出。

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.. 然后在你的命令行中调用它: grunt server wait然后你应该能够在浏览器中看到它..

Make sure you add it inside module.exports = function(grunt){...} 确保将其添加到module.exports = function(grunt){...}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM