TypeError: Router.use () requires an intermediate layer function, but receives an object

There were some middleware changes in the new version of Express, and I made some changes to my code in some other posts about this issue, but I can't get anything to stick.

We have worked this before, but I do not remember what a change is.

throw new TypeError('Router.use() requires middleware function but got a ^ TypeError: Router.use() requires middleware function but got a Object 



 node ./bin/www js-bson: Failed to load c++ bson extension, using pure JS version js-bson: Failed to load c++ bson extension, using pure JS version 



 /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) 

app.js

 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; 



It seems that the answer to this question has changed for versions. Thanks Nik

+133
javascript express
Dec 14 '14 at 2:45
source share
8 answers

If you use Express above 2.x, you must declare app.router as shown below. Please try replacing your code.

 app.use('/', routes); 

from

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

Please click here to get more information about app.router

thank

Remarks:

app.router is deprecated on express 3. 0+. If you are using express 3. 0+, see Anirudh's answer below.

+5
Dec 14 '14 at 3:42 on
source share

On any of your js pages you don't see

 module.exports = router; 

Check and check all your js pages

+748
Feb 07 '15 at 8:10
source share

A simple solution if you use express and do

 const router = express.Router(); 

make sure

 module.exports = router ; 

at the end of your page

+40
Apr 24 '18 at 17:34
source share

I am getting the same error message, but I have a different problem. Posting to others who are stuck on the same.

I transferred the get , post , put , delete functions to a new router file during refactoring and forgot to edit the paths. Example:

Wrong:

 //server.js app.use('/blog-posts', blogPostsRouter); //routers/blogPostsRouter.js router.get('/blog-posts', (req, res) => { res.json(BlogPosts.get()); }); 

Right:

 //server.js app.use('/blog-posts', blogPostsRouter); //routers/blogPostsRouter.js router.get('/', (req, res) => { res.json(BlogPosts.get()); }); 

It took a while to determine, because of an error I checked the syntax where I could wrap the argument in an object or where I skipped module.exports = router;

+2
Jan 11 '17 at 18:45
source share

I had this error and help solution that was sent by Anirudh. I built a template for express routing and forgot about this nuance - I am glad that it was easy to fix.

I wanted to give a little clarification of his answer on where to put this code, explaining my file structure.

My typical file structure is as follows:

 /lib /routes 

---index.js (controls the main navigation)

 /page-one /page-two ---index.js 

(each file [in my case index.js is inside page-2, although page-one will also have a .js index) - for each page - for which app.METHOD or router.METHOD should have module.exports = router; in the end)

If anyone wants to, I will post a link to a github template that implements express routing using best practices. let me know

Thanks Anirudh !!! for a great answer.

+1
Dec 16 '16 at 21:33
source share

Check out all of these files:

 var users = require('./routes/users'); var Users = require('./models/user'); var Items = require('./models/item'); 

Save correctly, in my case one file is skipped and the same error is thrown

+1
Jul 15 '18 at 20:00
source share

I think you did not export your router as your requirement.

module.exports = router; // what is the name of your router.

0
Dec 18 '17 at 10:25
source share

check your rout.js file

example my rout.js

  const express = require('express') const router = express.Router() const usersController = require('../app/controllers/usersController') const autheticateuser = require('../app/middlewares/authentication') router.post('/users/login', autheticateuser, usersController.login) router.post('/users/register', autheticateuser, usersController.register) 

check the end of the .js routes

module.exports = router

if not, add and module.exports = router run again




If your error is: " TypeError : Route.post () or Route.get () requires a middleware function, but received an object"

go to controller.js (i.e. usersController) and check all the function names that you may have made a mistake or indicated in the function routes file but missed in the controllers

 const User = require('../models/user') const express = require('express') const router = express.Router() module.exports.register = (req, res) => { const data = req.body const user = new User(data) user.save() .then((user) => { res.send(user) }) .catch((err) => { res.json(err) }) } 

in the .js routes I gave two routes, but in the controllers I skipped to determine the route for

router.post ('/ users / login')

it will make a mistake **

"TypeError: route.post () requires a middleware function, but received an object"

**

0
Jul 03 '19 at 17:31
source share



All Articles