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
Check If a String Is a Date Value
Generate Pdf from HTML in Div Using JavaScript
Import Image from Json Source (Create-React-App)
How to Change the Width of Angular Material Button
Node.Js Call Function If Button Clicked
React Native Dynamically Change View's Background Color
React Wait Until Set State in the Same Function
How to Execute a Function After Another One
Google Script to See If Text Contains a Value
Using Html5/Javascript to Generate and Save a File
How to Apply CSS to Parent Using Child Selector - With CSS or Angular 2
Calculate Time Difference Between Two Date in Hh:Mm:Ss JavaScript
Input Field Value Should Not Be Greater Than Other Field Value
How to Simulate Key Press Events Programmatically
React Native Flatlist With Clickable Items
How to Correctly Use Axios Params With Arrays
How Could I Get the Latest Date from an Array of Dates - Javascript/Typescript