简体   繁体   中英

Node.js: TypeError: Router.use() requires a middleware function but got a Object

hi m trying to get an index page but it is showing an error: TypeError: Router.use() requires a middleware function but got a Object, structure: views>attendance>index.ejs how can I resolve this error,

controller:

   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);

The only reason I can imagine for this not working is incorrect exports/imports.

You need to export the router like this in the controller file:

module.exports = router;

Also, by doing router.get('/attendance'... and applying it app.use('/attendance', , the application will listen for requests directed to /attendance/attendance endpoint. I'd change the controller to:

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

See this for reference: How to write clean, modular express.js applications

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM