Javascript Node.js:TypeError:Router.use()需要一个中间件函数,但得到了一个对象

Javascript Node.js:TypeError:Router.use()需要一个中间件函数,但得到了一个对象,javascript,node.js,node-modules,Javascript,Node.js,Node Modules,您好,我正在尝试获取索引页,但它显示了一个错误:TypeError:Router.use()需要一个中间件函数,但得到了一个对象,结构:views>attention>index.ejs如何解决此错误 控制器: in controller I write this path: router.get('/attendance', (req, res) => res.render('attendance/index')); app.js require('./models/A

您好,我正在尝试获取索引页,但它显示了一个错误:TypeError:Router.use()需要一个中间件函数,但得到了一个对象,结构:views>attention>index.ejs如何解决此错误

控制器:

   in controller I write this path:
   router.get('/attendance', (req, res) => res.render('attendance/index'));
app.js

  require('./models/Attendance');

  const attendanceController = require('./controllers/attendanceController');

  app.use('/attendance', attendanceController);

我能想到的唯一原因是不正确的导出/导入

您需要在控制器文件中导出路由器,如下所示:

module.exports = router;
另外,通过执行
router.get(“/attention”…
并应用它
app.use(“/attention”,
),应用程序将侦听指向/attention/attention端点的请求。我将控制器更改为:

router.get('/', (req, res) => res.render('attendance/index'));

参见此参考:

我添加了module.exports=router;