I have a users.js
and a index.js
file.
users.js
const express = require('express');
const router = express.Router();
const {catchErrors} = require('../handlers/errorHandlers');
const authController = require('../controllers/authController');
router.post('login/', catchErrors(authController.login));
module.exports.router = router;
index.js
// Route handlers
let userRouter = require('./users');
router.use('/user', userRouter);
module.exports = router;
I tried this and it's not working. I would appreciate any advice.
In your users.js
file you're exporting an object with the property router
( module.exports.router = router
) which would look like..
module.exports = {
router: router
}
and in your index you're importing the object from users.js
but not accessing the router when passing it to router.use(...)
.
You should pass the router in your index.js
file
const express = require('express');
const router = express.Router();
// You can access the router on the require
const userRouter = require('./users').router;
router.use('/user', userRouter);
// Or on the object after the require
const userRouter = require('./users');
router.use('/user', userRouter.router);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.