Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/Questions/Resolved the problem of the throw new typeerror('router.use() requires a middleware function but got a ' + gettype(fn));
Next
Answered
Julia Lelievre
  • 30
Julia Lelievre
Asked: May 12, 20222022-05-12T09:18:20+00:00 2022-05-12T09:18:20+00:00In: javascript

Resolved the problem of the throw new typeerror(‘router.use() requires a middleware function but got a ‘ + gettype(fn));

  • 30

Recently, I ran some of my javascript code, and it gave the warning text:

throw new TypeError('Router.use() requires middleware function but got a
  ^
 TypeError: Router.use() requires middleware function but got a Object
/Users/datis/Documents/bb-dashboard/node_modules/express/lib/router/index.js:438
  throw new TypeError('Router.use() requires middleware function but got a
  ^
 TypeError: Router.use() requires middleware function but got a Object
  at /Users/datis/Documents/bb-dashboard/node_modules/express/lib/router/index.js:438:13
  at Array.forEach (native)
  at Function.use (/Users/datis/Documents/bb-dashboard/node_modules/express/lib/router/index.js:436:13)
  at /Users/datis/Documents/bb-dashboard/node_modules/express/lib/application.js:188:21
  at Array.forEach (native)
  at Function.use (/Users/datis/Documents/bb-dashboard/node_modules/express/lib/application.js:185:7)
  at Object.<anonymous> (/Users/datis/Documents/bb-dashboard/app.js:46:5)
  at Module._compile (module.js:456:26)
  at Object.Module._extensions..js (module.js:474:10)
  at Module.load (module.js:356:32)

While searching, I realized that some people added some command lines in my sample above. But I don’t think it is the best way to correct the problem – throw new typeerror(‘router.use() requires a middleware function but got a ‘ + gettype(fn)); How would you explain this trouble? or Is there a better way? Below is the detail of the command that I used:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var session = require('express-session');
var MongoClient = require('mongodb').MongoClient;
var routes = require('./routes/index');
var users = require('./routes/users');

var Users = require('./models/user');
var Items = require('./models/item');
var Store = require('./models/store');
var StoreItem = require('./models/storeitem');

var app = express();
//set mongo db connection
var db = mongoose.connection; 

MongoClient.connect("mongodb://localhost:27017/test", function(err, db) {
  if(!err) {
    console.log("We are connected");
  }
});
// var MONGOHQ_URL="mongodb://localhost:27017/test" 

// view engine setup
app.set('views', path.join(__dirname, 'views'));

app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(session({
    secret: 'something',
    resave: true,
    saveUninitialized: true
}));

app.use('/', routes);
app.use('/users', users);
app.use(express.static(path.join(__dirname, 'public')));

// catch 404 and forward to error handler
// app.use(function(req, res, next) {
//     var err = new Error('Not Found');
//     err.status = 404;
//     next(err);
// });

// Make our db accessible to our router
app.use(function(req, res, next){
  req.db = db;
  next();
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});


module.exports = app;
typeerror
  • 2 2 Answers
  • 1k Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    dhtutoria Expert
    2022-06-01T13:55:38+00:00Added an answer on June 1, 2022 at 1:55 pm

    The cause: This error is caused by the fact that you are lacking the router exports module.

    Solution: Incase of using express above 2.x, you can do like that:

     change the code

    app.use('/', routes);

    into

    app.use(app.router);
    routes.initialize(app);
    • 10
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Raphaël Lacombe
    2022-05-25T19:37:38+00:00Added an answer on May 25, 2022 at 7:37 pm

    You are missing any of your js pages

    module.exports = router;

    Verify all JS pages.

    • 9
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.