1

I have index.js file like this and

const express = require('express');
const app = express();

//Import Routes
const authRoute = require('./routes/auth');

//Route Middlewares
app.use('/api/user', authRoute);

app.listen(3000, () => console.log('Server Up and running'));

auth.js like this one

const router = require('express').Router();

router.post('/register', (req, res) => {
    res.send('Register');
})
module.exports = router;

Why i get the TypeError('Router.use() requires a middleware function but got a ' + gettype(fn)) ^

TypeError: Router.use() requires a middleware function but got a Object at Function.use (C:\Users\xxx\Desktop\Websites\Authentication\node_modules\express\lib\router\index.js:458:13) at Function. (C:\Users\xxx\Desktop\Websites\Authentication\node_modules\express\lib\application.js:220:21)

roman
  • 51
  • 1
  • 2
  • 4
  • [TypeError: Router.use() requires middleware function but got a Object](https://stackoverflow.com/questions/27465850/typeerror-router-use-requires-middleware-function-but-got-a-object) ? – Andreas May 30 '20 at 13:32
  • sometimes i got this error when i was rewriting the route handler. i just restartet the application and it worked again – bill.gates May 30 '20 at 13:51
  • Restarting doesn't help – roman May 30 '20 at 14:04

1 Answers1

0

You may try to re-write your auth.js in the following format, I think that will solve the issue:

router.route('/register').post((req, res) => {  .............
CyberMessiah
  • 1,084
  • 11
  • 14