【问题标题】:Warning: connect.static is not a function Use --force to continue警告:connect.static 不是函数使用 --force 继续
【发布时间】:2016-01-02 20:22:20
【问题描述】:

我正在使用 YO lessapp 项目,“grunt-contrib-connect”帮助我在 9000 端口上启动节点 js 服务器。每当我运行 grunt serve(启动服务器)时,服务都会由于以下警告而中止。

Running "connect:livereload" (connect) task
Warning: connect.static is not a function Use --force to continue.

确切的错误发生在 Gruntfile.js 中的以下函数中

 livereload: {
        options: {
          middleware: function(connect) {
            return [
              connect.static('.tmp'),
              connect().use('/bower_components', connect.static('./bower_components')),
              connect.static(config.app)
            ];
          }
        }
      }, 

我已经安装了 npm install grunt-contrib-connect --save-dev, npm install serve-static --save-dev

我遇到了一些帖子,有些建议关闭防火墙但没有运气。

我知道这与我的机器或 npm/node/connect 版本冲突有关,因为我尝试从其他机器运行相同的应用程序并且它运行良好。

系统配置:

  • Windows 7 专业版
  • 节点-v4.1.2
  • npm -v2.14.4
  • connect@3.4.0

我已经根据nodejs connect cannot find static 的帖子安装了 connect 和 serve-static,但还是一样

有什么帮助吗?提前致谢

【问题讨论】:

  • 感谢您的解决方案,它需要在 grunt 中使用 serve-static。

标签: gruntjs npm grunt-contrib-connect


【解决方案1】:

你必须安装connectserve-static

npm install --save-dev grunt-contrib-connect serve-static 

然后你必须在Gruntfile.js中导入serve-static

module.exports = function (grunt) {
  ...
  var serveStatic = require('serve-static');

  grunt.initConfig({
  ...
    connect: {
    ...
      livereload: {
        options: {
          middleware: function(connect) {
            return [
              serveStatic('.tmp'),
              connect().use('/bower_components', serveStatic('./bower_components')),
              serveStatic(config.app)
            ];
          }
        }
      }

【讨论】:

    【解决方案2】:

    从 0.11.x 版本开始,新的grunt-contrib-connect 不支持connect.staticconnect.directory
    您应该安装serve-static(用于提供静态文件)和serve-index(用于提供包含给定路径的目录列表的页面)。

    像这样:
    var serveStatic = require('serve-static');
    var serveIndex = require('serve-index');

    使用serveStatic 代替connect.static

    serveIndex 而不是connect.directory

    grunt.initConfig({
        connect: {
            options: {
                test: {
                   directory: 'somePath',
                   middleware: function(connect, options){
                        var _staticPath = path.resolve(options.directory);
                        return [serveStatic(_staticPath), serveIndex(_staticPath)]
                   }
                }
            }
        }
    })
    

    【讨论】:

    • 按照这个答案,对我有用的是require('serve-static')(.tmp)
    猜你喜欢
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-08
    • 2016-08-27
    • 1970-01-01
    • 2017-06-22
    • 2016-10-06
    相关资源
    最近更新 更多