connect argument is undefined under middleware function

為{幸葍}努か 提交于 2019-12-14 04:08:01

问题


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:


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')); },
...]


来源:https://stackoverflow.com/questions/33254376/connect-argument-is-undefined-under-middleware-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!