Sunday, 14 May 2017

How to use the routes correctly using express.Router?

var x = require('./usefile/contacts');

var reqAuth = passport.authentication('jwt',{session : false});
var reqLogin = passport.athentication('local',{session: false});



module.exports = function(app){


var player = express.Router();   
var sportsman = express.Router(); 

app.use('/api/auth', player);

player.post('/register', reqLogin, function(req, res) { //not working
x.register});

player.post('/login', reqLogin, function(req, res) {   //not working.
x.login});


 app.use('/api/contacts', sportsman);

  sportsman.get('/' , x.login); //this is working.but when I put .get('/', req.Auth,x.login) then it shows unauthorised as I have not added any userid and password.)


    sportsman.post('/p', requireAuth, function (req, res) {  //not working.
        console.log("inside post routes of contacts");
        x.postContacts}
    }

Note:

1.)I have added the question and statements in the comments.contacts folder contains my login and register functions that are exported here.

2.)When I use express.Router().post('/p',requireAuth, x.register) which is similar to .get I have used in the program, it shows error displaying that post conatins a callback function.



via Aditya Jain

No comments:

Post a Comment