Router.use()需要一个中间件的功能,但在我的节点文件中得到了一个对象
新来的节点,请解决错误Router.use()需要一个中间件的功能,但有一个对象
const bodyParser = require('body-parser');
const express = require('express');
const app = express();
var productRoutes = require('./api/routes/product');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}))
app.use('/products', productRoutes);
module.exports = app;
api/routes/product.js
:
const express = require('express');
const router = express.Router();
router.get('/', (req, res, next) => {
res.status(200).json({message: "Here we are handling the get request for the products"});
});
回答如下:
你改变qazxsw POI代码。将正常工作。
api/routes/product
原因是你没有导出你的路由器到其他文件时,你写在文件const express = require('express');
const router = express.Router();
router.get('/', (req, res, next) => {
res.status(200).json({
message: "Here we are handling the get request for the products"
});
});
module.exports = router;
则将导出。