Middleware

//Routes let users = require("../controllers/userController"); module.exports = function(app){ // Users app.post('/api/users/authenticate', users.authenticate) app.post('/api/users/create', users.create) app.get('/api/users/users', users.users) app.get('/api/users/:id', users.user) // app.put('/api/users/edit', users.edit) //Token Validations app.get('/api/me',users.authenticateToken) app.use(users.middleWare) } //controller module.exports.authenticateToken = function(req, res, next){ var token = req.headers['authorization'] if (!token) return res.status(401).send( { auth: false, message: 'No token provided.' }); jwt.verify(token, secret, function(err,decoded){ if(err){ console.log(err) res.json({ message: "Failed to authenticate token." }); }else{ User.findById(decoded.id,function(err,user){ if(err) return res.status(500).send("There was a problem finding the user."); if(!user) return res.status(404).send("No user found."); req.user = user next(user); }); } }); } module.exports.middleWare = function(user, req, res, next) { res.send(user); }; module.exports.user = function(req,res,next){ console.log(req.user) User.findById(req.user,function(err,user){ if(err){ console.log(err) }else{ console.log(user); } }) }
I want it to call the middleware whenever '/api/users/:id' is called, but it's not working. Any help would be much appreciated.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.