【问题标题】:grunt server can't be connected <gruntjs>grunt 服务器无法连接 <gruntjs>
【发布时间】:2012-10-31 19:28:18
【问题描述】:
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.

完成,没有错误。

但无法通过输入[http://127.0.0.1:8888][1] in browsers ! jiong~连接

如何在 windows 或 unix 中解决这个问题?

【问题讨论】:

  • 修复了吗,或者你知道为什么吗?
  • 我有同样的行为,很想知道是否有人知道这一点。

标签: javascript linux html node.js gruntjs


【解决方案1】:

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

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

【讨论】:

  • 这似乎是最有建设性的答案。
  • 这是我的救命稻草。
【解决方案2】:

不要使用 grunt 服务于您的项目。 Grunt 是一个构建工具。相反,请使用 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"
    }
}

现在您可以运行npm start,生活将变得美好。 Grunt 是一个构建工具,而不是一个服务器。 npm 是包生命周期管理器,而不是构建工具。 Express 是一个服务器库。在正确的位置使用每个。

跟进(2013-08-15)

此规则的例外情况是您需要将项目提供给构建堆栈中的其他测试工具。 grunt-contrib-connect 插件是专门为这个用例而设计的,并且有一个 keepalive 配置设置,可以在提供静态文件时让 grunt 保持打开状态。这通常与watch 任务结合使用,该任务在测试或代码更改时运行测试套件。

【讨论】:

  • 我知道这是旧的,但是如果你使用 express.static('.') 子目录(例如 css、js 等)会得到 403 错误。请改用 express.static(__dirname)。
  • 最佳实践绝对正确。编辑答案以使用 __dirname。
  • Grunt 并不一定局限于“构建工具”。 Grunt 是一个任务运行器(在其网站上自称为“JavaScript 任务运行器”),类似于 Ruby 中的 Rake,它恰好对运行项目构建非常有用。 github.com/gruntjs/grunt-contrib-connect 任务的选项甚至有一个 keepalive 选项,用于将持久连接服务器作为 grunt 任务运行。
  • 在教学上,在这个问题的背景下,我觉得提供一个黑白答案更有用。也许最初的 Q 确实是在测试的上下文中 - 将 grunt 作为服务器运行的最常见和非常有用的理由 - 但我没有从 Q 中得到这一点,并且发现带走选项更合适,直到提问者使用这些工具更舒服。
  • +1 用于教学法的使用 :),澄清并谨慎行事。
【解决方案3】:

server 任务仅在需要时运行,但您可以阻止它退出。来自a comment 和widget 的另一个问题:在您的grunt.js 文件中定义一个名为run 的任务,该任务运行server 和watch 任务。

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

watch 任务无限期运行,因此它会阻止 server 任务结束。只要确保你也有watch 任务的配置。这一切都在您的grunt.js 文件中:

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

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

从命令行输入:

$ grunt run

服务器将保持正常运行。

或者,正如@NateBarr 指出的那样,您可以从命令行运行:

$ grunt server watch

【讨论】:

  • 或者只是做$ grunt server watch
  • 谢谢,内特。添加到答案。
【解决方案4】:

默认情况下,Grunt 启动服务器只是为了测试(或任何其他要求的任务..),一旦完成,它就会退出......

但幸运的是,我找到了一个解决方案,通过将其添加到您的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); }
});

然后在你的命令行中调用它:grunt server wait 然后你应该能够在浏览器中看到它..

确保将其添加到 module.exports = function(grunt){...}

【讨论】:

  • 谁反对这个? “Grunt 启动服务器只是为了测试(或要求的任何其他任务..),一旦完成,它就会退出......”这是一个很好的信息。
猜你喜欢
  • 2014-09-03
  • 1970-01-01
  • 1970-01-01
  • 2019-10-08
  • 2020-04-06
  • 2020-11-03
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多