grunt服务器无法连接<gruntjs>

use*_*849 13 javascript linux html5 node.js 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.
Run Code Online (Sandbox Code Playgroud)

完成,没有错误.

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

如何在windows或unix中修复此问题?

Sin*_*hus 26

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

grunt.initConfig({
  connect: {
        target:{
            options: {
                port: 9001,
                keepalive: true
            }
        }
    }
});
Run Code Online (Sandbox Code Playgroud)


Dav*_*her 5

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

server.js

var express = require("express"),
    app = express();
app.use('/', express.static(__dirname));
app.listen(8888);
Run Code Online (Sandbox Code Playgroud)

package.json

{
    "name": "my-project",
    "scripts": {
        "start": "node server.js"
    },
    "dependencies": {
        "express": "3"
    }
}
Run Code Online (Sandbox Code Playgroud)

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

跟进(2013-08-15)

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

  • 我知道这是旧的,但如果你使用express.static('.')子目录(如css,js等)将得到403错误.请改用express.static(__ dirname). (2认同)

ada*_*ign 0

默认情况下,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); }
});
Run Code Online (Sandbox Code Playgroud)

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

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