node.js - NodeJs - Refactored Authenticate middleware callback error -


i experiencing refactoring issue , cannot see how correct matter:

1. section of code, not refactored, works properly:

'use strict';   // use svcs when refactoring   var svcs = require('../../../services/ensure-authenticated.server.service'),    config = require('../../../config/config.server.config.js'),   ctrl = require('../../../controllers/index.server.controller'),   express = require('express'),   router = express.router();    // remove when refactoring   var jwt = require('jwt-simple');   var moment = require('moment');   // refactored  function ensureauthenticated(req, res, next) {     if (!req.headers.authorization) {         return res.status(401).send({message: 'please make sure request has authorization header'});     }     var token = req.headers.authorization.split(' ')[1];     var payload = jwt.decode(token, config.token_secret);     if (payload.exp <= moment().unix()) {         return res.status(401).send({message: 'token has expired'});     }     req.user = payload.sub;     next();}   module.exports = function(app){      router.route('/me')       // middleware change when refactored       .get(ensureauthenticated, ctrl.userctrl.getme)       .put(ensureauthenticated, ctrl.userctrl.putme);    app.use('/api', router);  } 

2a. if refactor ensure authenticate:

'use strict';  var config = require('../config/config.server.config.js'); var jwt = require('jwt-simple'); var moment = require('moment');  var self = {   ensureauthenticated: function (req, res, next) {     if (!req.headers.authorization) {         return res.status(401).send({message: 'please make sure request has authorization header'});     }     var token = req.headers.authorization.split(' ')[1];     var payload = jwt.decode(token, config.token_secret);     if (payload.exp <= moment().unix()) {         return res.status(401).send({message: 'token has expired'});     }     req.user = payload.sub;     next();   } }  module.exports = self; 

2b. route looking this:

'use strict'; var svcs = require('../../../services/ensure-authenticated.server.service.js'), ctrl = require('../../../controllers/index.server.controller.js'), express = require('express'), router = express.router();   module.exports = function(app, config){ router.route('/me')     .get(svcs, ctrl.userctrl.getme)     .put(svcs, ctrl.userctrl.putme); app.use('/api', router); } 

i following error:

 error: route.get() requires callback functions got [object object] 

the code has not changed in least, has refactored , cannot see why happening.

does see why happening , how correct?

many thanks.

silly mistake on part:

   .get(svcs.ensureauthenticated, ctrl.userctrl.getme)    .put(svcs.ensureauthenticated, ctrl.userctrl.putme); 

Comments

Popular posts from this blog

How to run C# code using mono without Xamarin in Android? -

c# - SharpSsh Command Execution -

python - Specify path of savefig with pylab or matplotlib -