//server.js file
const express = require('express');
const app = express();
const PORT = 8888;
const bodyParser = require('body-parser');
const userRoutes = require("./routes/user");
const adminRoutes = require("./routes/admin");
const userList = require("./public/model/api/userList");
app.use(bodyParser.urlencoded({ extended: true }));
app.use('/user',userRoutes);
app.use('/admin',adminRoutes);
app.use('/userList',userList);
app.listen(PORT,()=>{
console.log("server is runing on port:",PORT);
})
//route file ,admin.js
const express = require("express");
const reqFilter = require('../public/model/middleware');
const app = express();
const route = express.Router();
var userData = require('../public/model/api/userList');
route.use(reqFilter);
app.post("/users",(req,res)=>{
console.log("no 1");
res.send({data:"Admin get user is here!"});
});
route.post("/list",(req,res)=>{
console.log(userData.userlist);
res.send({data:"Admin post user is here now!"});
});
app.use('/',route);
module.exports = app;
//other file ,,, userList.js
exports.userlist = function(req,res){
res.send("this is userlist");
}
//errore -> TypeError: Router.use() requires a middleware function but got a Object
what is the problem please help me to solve this...
i have a problem with separate route from separate folder.. and dont call other file method using separate route ..
CodePudding user response:
You are not importing reqFilter
correctly. You need to change
const reqFilter = require('../public/model/middleware');
to
const { reqFilter } = require('../public/model/middleware');
or
const middleware = require('../public/model/middleware');
route.use(middleware.reqFilter);
CodePudding user response:
Router.js
import userList from "./userList";
import mymiddleware from "./mymiddleware";
const router = express.Router();
router.route("/").post(mymiddleware, createTransaction);
module.exports = router;
userList.js;
exports.userlist = function (req, res) {
Users.findOne({})
.then((users) => {
res.status(200).json(users);
})
.catch((error) => {
next(error);
});
};
export default getTransaction;