Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am getting an error in my node Web API project.

Error :-
TypeError: Router.use() requires a middleware function but got a Object


It is in index.js file and code follows.

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


const feeSchedules = require('./feeschedules.routes');
const claimEdits = require('./claimedits.routes');
const crossWalks = require('./crosswalk.routes');
const codings = require('./coding.routes');
const references = require('./reference.routes');
const authenticate = require('./authentication.routes');

router.use('/feeschedules',feeSchedules);
router.use('/ClaimEdits',claimEdits);
router.use('/Crosswalks',crossWalks);
router.use('/Codings',codings);
router.use('/References',references);
router.use('/Access',authenticate);

module.exports=router;


If a Comment on last router.use code i.e.
router.use('/Access',authenticate);
then error is vanishing. Breaking head from past 12 hours.

Please help.

Thanks in advance.

What I have tried:

Tried multiple ways.

If a Comment on last router.use code i.e. <pre>router.use('/Access',authenticate);
then error is vanishing. Breaking head from past 12 hours.
Posted
Updated 28-Jul-19 17:08pm

1 solution

First of all, this seems to be a bad coding style, having a method called "authenticate" required form something called "authentication.routes", the first is a verb, the second the plural of a noun. Or better to say by name, the first is a method, the secound an object. So Maybe you are indeed returning an object somethere in "authentication.routes".

Now, more likely, read this[^]
your export is wrong.

module.exports = { 
  router:router
}

var blabla = require('./routes/blabla.js');
app.use(blabla.router)


And, by the way, the reason the authentification route fails with this error is, because athentification comes first. In other words, this is the first route to be evaluated, the other would fail too, but you never get to that point, right?
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900