有一些中间件的变化在新版本的表达,我已经做了一些改变,在我的代码周围的一些其他职位对这个问题,但我不能得到任何坚持。请帮帮我。这是我的index.js文件
const express = require("express");
const app = express();
const dotenv = require("dotenv");
const mongoose = require("mongoose");
const router = express.Router();
dotenv.config("");
//connect to db
mongoose.connect(
process.env.DB_CONNECT,
{ useUnifiedTopology: true, useNewUrlParser: true },
()=> console.log("connected to db")
);
//IMPORT ROUTES
const productRoutes = require("./routes/product");
//routes middlewars
app.use("./api/products", productRoutes);
app.listen(4000, () => console.log("server is running on port 4000!"));
module.exports = router ;
和投掷错误
TypeError: Router.use() requires a middleware function but got a Object
at Function.use (C:\Users\HP\node-api\node_modules\express\lib\router\index.js:469:13)
at Function.<anonymous> (C:\Users\HP\node-api\node_modules\express\lib\application.js:22
7:21)
at Array.forEach (<anonymous>)
at Function.use (C:\Users\HP\node-api\node_modules\express\lib\application.js:224:7)
at Object.<anonymous> (C:\Users\HP\node-api\index.js:21:5)
at Module._compile (node:internal/modules/cjs/loader:1159:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
at Module.load (node:internal/modules/cjs/loader:1037:32)
at Module._load (node:internal/modules/cjs/loader:878:12)
Node.js v18.12.1
error is
1条答案
按热度按时间tp5buhyn1#
您可能没有正确导出产品路线。
但是,如果您最终没有在文件中使用快速路由器,为什么要在index.js中导入和导出它呢?