Typeerror: Router.Use() Requires Middleware Function But Got a Object

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

If your are using express above 2.x, you have to declare app.router like below code. Please try to replace your code

app.use('/', routes);

with

app.use(app.router);
routes.initialize(app);

Please click here to get more details about app.router

Note:

app.router is depreciated in express 3.0+. If you are using express 3.0+, refer to Anirudh's answer below.

Router.use() requires a middleware function but got a ' + gettype(fn))

You are mistakenly using app.use() instead of app.set().

Change this:

app.use('port', process.env.PORT || 3000)

to this:

app.set('port', process.env.PORT || 3000)

The error comes because you're passing a string or number to app.use() where it expects a middleware function reference.

It seems like you should have had a stack trace for this error (if you learn how to interpret it) that points to the exact line of code (a few steps up the stack) causing the problem which should simplify debugging next time.

TypeError: Router.use() requires a middleware function but got a string at Function.use

You need to use

app.set("view engine","jade");

instead of

app.use("view engine","jade");

as you're intending to set the view-engine property to jade, not setting up a middleware.

How to solve "Router.use() requires a middleware function but got a Object at Function.use" error

You are using a module that you never exported.

app.use('/api', route);

but route was never exported - that's your main issue.

in your route module you can wrap everything in export default - alternatively you can use module.exports = router

This is an open source project I was working on try following the structure, if you still have issues let me know.

https://github.com/Muhand/nodejs-server

Router.use() requires a middleware function but got undefined

Change all of these:

var registrationRouter = require('./routes/registration-route').default;

to remove the .default so you just have this:

const registrationRouter = require('./routes/registration-route');

.default is something that is used with ESM modules, not with CommonJS modules.


Also, you shouldn't be using var any more. It's obsolute now. Use const for these.



Related Topics



Leave a reply



Submit