简体   繁体   English

如何解决“ TypeError:Router.use()需要中间件功能但有一个对象”

[英]How to fix “TypeError: Router.use() requires a middleware function but got a Object”

I am having an error with an express routing app 我在快速路由应用中出错

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

router.get('/test', (req, res) => res.json({ msg: 'Users Works' }));

module.exports = router;

TypeError: Router.use() requires a middleware function but got a Objec TypeError:Router.use()需要一个中间件功能,但是得到一个对象

This error occurs if there is no // module.exports = router; 如果没有// // module.exports =路由器,则会发生此错误; // //

If it error still continues, then you might have a middleware that is not yet set up that executes before this route. 如果错误仍然存​​在,则可能是您尚未设置的中间件在此路由之前执行。

Export Router From First File like this. 像这样从第一个文件导出路由器。

const express = require("express");
const router = express.Router();
router.get("/", async (req, res) => {
  const result = await Post.find();
  res.status(200).json({
    message: "Some Data",
  });
});
module.exports = router;

Then Import in other root files like this and use 然后像这样导入其他根文件并使用

const express = require("express");
const app = express();
const todo = require("./routes/todo/todo");
app.use("/todo", todo);

For Reference Complete Nodejs Routing App 供参考完整的Node.js路由应用程序

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError: Router.use() 需要一个中间件函数但是得到了一个对象 - TypeError: Router.use() requires a middleware function but got a Object TypeError: Router.use() 需要中间件函数但得到了一个对象 - TypeError: Router.use() requires middleware function but got a Object 获取TypeError:Router.use()需要一个中间件功能,但未定义 - Getting TypeError: Router.use() requires a middleware function but got a undefined TypeError:Router.use()需要一个中间件功能,但未定义 - TypeError: Router.use() requires a middleware function but got a undefined TypeError-Router.use()需要中间件功能,但未定义 - TypeError - Router.use() requires a middleware function but got a undefined Express 路由器:Router.use() 需要中间件 function 但得到了 Object - Express router : Router.use() requires a middleware function but got a Object TypeError: Router.use() 需要一个中间件 function 但得到了一个对象 [using express router] -- - TypeError: Router.use() requires a middleware function but got a Object[using express router] -- Router.use()需要中间件功能,但有一个对象 - Router.use() requires middleware function but got an Object Node.js:TypeError:Router.use()需要中间件功能,但有一个对象 - Node.js: TypeError: Router.use() requires a middleware function but got a Object node:js TypeError:Router.use()需要一个中间件功能,但有一个Object - node:js TypeError: Router.use() requires a middleware function but got a Object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM