When trying to import requireSignin
from the controllers/auth.js
file into the routes/user.js
file and adding it to the
router.get('/user/:id', requireSignin, read);
route, an error occurs:
Error: Route.get() requires a callback function but received an [object Undefined]
controllers/auth.js
const expressJwt = require('express-jwt');
exports.requireSignin = expressJwt({
secret: process.env.JWT_SECRET
});
routes/user.js
const express = require('express');
const router = express.Router();
const {requireSignin} = require('../controllers/auth');
const {read} = require('../controllers/user');
router.get('/user/:id', requireSignin, read);
module.exports = router;
Interestingly, when I directly include the requireSignin
function within the same file routes/user.js
, everything works smoothly.
routes/user.js
const express = require('express');
const router = express.Router();
const expressJwt = require('express-jwt');
const {read} = require('../controllers/user');
router.get('/user/:id', expressJwt({
secret: process.env.JWT_SECRET
}), read);
module.exports = router;