中间件功能下连接参数未定义 [英] connect argument is undefined under middleware function

查看:163
本文介绍了中间件功能下连接参数未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的连接配置为 - 咕噜-contrib-connect

  connect:{
options:{
...
},
代理:[{...
}],
livereload:{
选项:{
base:gruntTargetPath,
open :true,
中间件:函数(连接,选项,中间件){
返回[
...,
connect.static('。tmp'),
connect()。use('/ bower_components',connect.static('./ bower_components')),
connect.static(config.app)
];
}
}
}
}

我在bash上执行 - grunt connect:livereload ,它提示 -

 警告:undefined不是函数使用--force继续。 

由于警告而中止。

(它与 connect 参数有关,我检查了它)。



如何正确传递这个参数?

解决方案

看起来你在调用connect而不是在这里引用它:

connect()。use



应该是 connect.use

更新



查看文档,中间件功能应该返回一个带有签名(req,res,next)的函数数组,但是目前您正在传递来自的返回值数组use 静态方法,可以是任何东西。



他们的例子将函数插入到middlewars数组中然后返回:

  middlewares.unshift(function(req,res,next){
if(req。 url!=='/ hello / world')return next();

res.end('Hello,world from port#'+ options.port +'!');
});

返回中间件;

如果您想坚持使用您当前的返回数组文本的方法,请确保每个数组项都是一个带有预期签名的函数:

pre $ $ $ $ $ $ $ $ $ $ $ $ $ .static( 'TMP'); },
函数(req,res,next){connect.use('/ bower_components',connect.static('./ bower_components')); },
...]


I have connect configuration for - grunt-contrib-connect

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

When I execute on bash - grunt connect:livereload , it prompts -

 Warning: undefined is not a function Use --force to continue.

Aborted due to warnings.

(it's regarding to the connect argument , I checked it) .

How to pass this argument correctly ?

解决方案

Looks like you're calling connect rather than referencing it here:

connect().use

Should be connect.use

Update

Looking at the documentation, the middleware function should return an array of functions with the signature (req, res, next) but currently you're passing an array of return values from the use and static methods which could be anything.

Their example inserts functions into the middlewars array and then return it:

middlewares.unshift(function(req, res, next) {
            if (req.url !== '/hello/world') return next();

            res.end('Hello, world from port #' + options.port + '!');
          });

          return middlewares;

If you want to stick with your current approach of returning an array literal, make sure each array item is a function with the signature expected:

[...,
function (req, res, next) { connect.static('.tmp'); },
function (req, res, next) { connect.use('/bower_components', connect.static('./bower_components')); },
...]

这篇关于中间件功能下连接参数未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆