javascript grunt 服务器无法连接 <gruntjs>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13342606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:29:57  来源:igfitidea点击:

grunt server can't be connected <gruntjs>

javascriptlinuxhtmlnode.jsgruntjs

提问by user1817849

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~

How about to fix this problem in windows or unix ?

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

回答by Sindre Sorhus

In grunt 0.4 combined with grunt-contrib-connectyou can run a long running server by using the keepaliveargument: grunt connect:target:keepaliveor 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
            }
        }
    }
});

回答by David Souther

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

不要使用 grunt 来为你的项目服务。Grunt 是一个构建工具。相反,使用 npm 生命周期脚本。

server.js

server.js

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

package.json

package.json

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

Now you can run npm startand 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.

现在你可以跑步了npm start,生活会很美好。Grunt 是一个构建工具,而不是一个服务器。npm 是一个包生命周期管理器,而不是一个构建工具。Express 是一个服务器库。在正确的位置使用每个。

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-connectplugin is designed specifically with this use case in mind, and has a keepaliveconfiguration setting that will leave grunt open while serving your static files. This is usually used in conjunction with a watchtask that runs a test suite when either the tests or the code changes.

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

回答by Chris Calo

The servertask only runs as long as it is needed, but you can keep it from quitting. From a commentby widgeton another question: In your grunt.jsfile define a task named runthat runs the tasks serverand watch.

server任务仅在需要时运行,但您可以防止它退出。从评论小部件上的另一个问题是:在你的grunt.js文件中定义了一个名为任务run是运行的任务serverwatch

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

The watchtask runs indefinitely, so it prevents the servertask from ending. Just make sure you also have a config for the watchtask. Here it is all together in your grunt.jsfile:

watch任务无限期运行,因此它可以防止server任务从结束。只需确保您也有该watch任务的配置。这是您的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 adardesign

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 addingthis to your grunt.jsfile 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 waitthen 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){...}