【问题标题】:"index.html" is not served by grunt-contrib-connectgrunt-contrib-connect 不提供“index.html”
【发布时间】:2014-12-31 05:28:33
【问题描述】:

我的代码很简单,而 grunt 文件是,

module.exports = function(grunt) {

   var proxySnippet = require('grunt-connect-proxy/lib/utils').proxyRequest;

   grunt.initConfig({
      connect: {
         server: {
            options: {
               hostname: "localhost",
               keepalive: true,
               base:['../web/'],
               port: 8080,
               middleware: function(connect, options) {
                  return [proxySnippet];
               },
               debug: true
            }
         }
      }
   });

   // grunt.loadNpmTasks('grunt-connect-proxy');
   grunt.loadNpmTasks('grunt-contrib-connect');

   grunt.registerTask('default', [

      'connect:server'
   ]);

};

有一个 index.html,路径是“../web/index.html”。当我打开

"http://localhost:8080"

它给出“无法获取 /”。知道为什么会这样吗?

【问题讨论】:

  • 当你直接去http://localhost:8080/index.html 时,它有效吗?如果不是,那么我会仔细检查 base 的值。无论如何,你为什么需要proxySnippet
  • 不,它也不起作用。我跟着一些例子,不需要proxySnippet?

标签: javascript gruntjs grunt-contrib-connect


【解决方案1】:

目录结构如下:

-node_modules
-templates
---index.html
---login.html
-Gruntfile.js

connect: {
         options: {
                port: 9000,
                livereload: true,
                hostname: 'localhost',
         },
         livereload: {
              options: {
                    open: true,
                    base: ['templates/']
              }
         }
    }

这会在浏览器上打开templates的完整目录结构以导航到任何html页面。

【讨论】:

    【解决方案2】:

    删除中间件代理,如下所示:

    module.exports = function(grunt) {
    
       var proxySnippet = require('grunt-connect-proxy/lib/utils').proxyRequest;
    
       grunt.initConfig({
          connect: {
             server: {
                options: {
                   hostname: "localhost",
                   keepalive: true,
                   base:['../web/'],
                   port: 8081,
    
                   debug: true
                }
             }
          }
       });
    
       // grunt.loadNpmTasks('grunt-connect-proxy');
       grunt.loadNpmTasks('grunt-contrib-connect');
    
       grunt.registerTask('default', [
    
          'connect:server'
       ]);
    
    };
    

    而且必须工作。

    【讨论】:

      猜你喜欢
      • 2015-01-25
      • 1970-01-01
      • 2017-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多