简体   繁体   中英

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

I am getting this error when I run npm start to run my express app.

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

I found the answer in the comments from Kop4lyf :

check your users.js. It should also be exporting the router like index.js, if you can try that.

However, this question was my top search result when I ran into this issue, so I am promoting to an answer.


The error is caused because one of your route modules is not being exported - meaning Express does not have access to it when it tries to identify all of your routes.

You can fix this by adding module.exports = router; to the end of each of your route files.

Example:

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

router.get('/', function(req, res, next) {
     //Do whatever...
});

module.exports = router;

More information about module.exports can be found on this question or the offcial Node.js documentation .

I have fixed this by adding which i am using somewhere. So please check your all exports.

module.exports = router;

If you use in routes

exports default router

Your solution can be

module.exports = router

Your index.js file is fine you just have to create users.js and export it.

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

  //Login Page - GET REQUEST
   router.get('/login',(req,res)=> {
      res.send('login page');
  })


  //Register Page - GET REQUEST
  router.get('/register',(req,res)=> {
     res.send('register page');
  });

  module.exports = router;
in every module  **export the router** and **keep one handler for the default 
path '/'**

// in index.js
const authRoute = require("./routes/authRoute");

app.use("/auth", authRoute);


// in authRoute.js

const express = require("express");
const router = express.Router();
router.get("/", (req, res) => {
    // code
 });

module.exports = router;

I had the same error , fixed it by replacing app.use('view engine', 'ejs') with app.set('view engine', 'ejs') . For reference I used this webpage Migrating from 3.x to 4.x

I didn't have to make any changes to either index.js or application.js. For more information on EJS one could refer Using EJS with Express and Express 4.x API

This error comes when you forgot to export the module which uses the Router.

Your mentioned code works perfectly with some tweaks.

if your app.js is main/starting point of the app.

it should have

const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listening on port ${port}...`));

instead of

module.exports = app;

(optional)Generally index.js is used for starting point of app. Rename index.js as helloworld.js and change same at require statement

var routes = require('./routes/index');

to

var routes = require('./routes/helloworld');

run this app using the following command

node app.js

If you have checked all the solution than also having this error than check this one

Another cause of having this error is calling a method which is not exist or not not exported. In my case i am calling login method but i forgot to define them

I was trying to call this method

app.post('/api/login', db.login);

but i had forgot to create login method so i got this error. also try to check spelling mistake may be you might have typed wrong spell

I had the same problem, and then I discovered that I was missing this line in one of my controllers !

return api; //it might be return router for your code !

I added this line to my code and it worked fine.

哇,我的问题是我在做 module.exports = { router } 而不是 module.exports = router

I found it after lot of struggle! as everything syntactically correct, nothing wrong with code that was written, it was due to the code that was not written yet! This could happen if you have implemented index.js but not yet users.js. However, you have already defined both lines app.use('/', routes); app.use('/users', users); If you are eager to test index.js right away without waiting for users.js to be implemented. That's exactly when it errors out.

如果您仍然面临这个问题并尝试所有解决方案,那么只需用路由替换路由器,它就可以正常工作

对于使用 ejs 的任何人:在我的情况下,我收到此错误,因为我使用了 app.use("view engine","ejs") 而不是 app.get("view engine","ejs")。

"

I fixed it by removing the app.use(/users, users);

I don't need this at the minute so maybe that is why it started breaking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM