Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Express Router.use()需要中间件函数,但得到了一个对象_Express_Router - Fatal编程技术网

Express Router.use()需要中间件函数,但得到了一个对象

Express Router.use()需要中间件函数,但得到了一个对象,express,router,Express,Router,将路由添加到express处理程序后,出现错误(请参见下文): 这里是文件夹“elasticsearch”中的index.js: 这是控制器中“writeIndex”函数的开始,我也没有看到任何错误: exports.writeIndex = function (req, res) { .. } function indexData(technicians,indexName, typeName) { .. } .. 我正在使用HEROKU,以前没有任何问题。 我认为这里的主要错误是: thr

将路由添加到express处理程序后,出现错误(请参见下文):

这里是文件夹“elasticsearch”中的index.js:

这是控制器中“writeIndex”函数的开始,我也没有看到任何错误:

exports.writeIndex = function (req, res) {
..
}
function indexData(technicians,indexName, typeName) {
..
}
..
我正在使用HEROKU,以前没有任何问题。 我认为这里的主要错误是:

throw new TypeError('Router.use() requires middleware function but got a ' + gettype(fn));
但这意味着什么

以下是我的项目目录结构(只是可读的部分):


在这里,您需要更正路径

app.use('/api/elasticsearch', require('../elasticsearch/index.js'));

我也有同样的问题,因为我没有补充

module.exports = router; 
在my routes/index.js的底部,确保导出为路由器

import {body, validationResult} = require('express-validator');
因此,如果您使用的是express validator,请使用
body()
而不是
check()

例如:-

使用,

body('email').isEmail();
check('email').isEmail();
而不是,

body('email').isEmail();
check('email').isEmail();
此处
电子邮件
是您的字段名


并检查您是否正在导出路由器。

这意味着您正在向路由传递对象,而不是中间件功能。如何定义“require('../elasticsearch')?该文件中的modules.export实际导出什么?(提示:导出对象不是函数)此“require”需要一个目录,因此应调用我在上面发布的文件“elasticsearch”目录中的index.js。顺便说一句:当我使用“app.use”时,我声明的路由不是文件系统中的路由,而是一些我希望用于请求的路由,对吗?/app/server/routes/index.js:16:9这是添加的路由:app.use('/api/elasticsearch',require('../elasticsearch');我添加了目录结构进行澄清。这解决了我的问题,谢谢!但问题依然存在,为什么其他路线的行为符合预期,而只有这条路线没有。。。
body('email').isEmail();
check('email').isEmail();