connect argument is undefined under middleware fun

2019-09-13 18:19发布

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 ?

1条回答
叛逆
2楼-- · 2019-09-13 18:56

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')); },
...]
查看更多
登录 后发表回答