[英]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.