0

I am very new to MEAN.io

Below Function(authDN) written & which is working fine on running alone like below.

when I run the function directly I am getting proper response in the console

authDN('myuserName', 'myPassword', output);

But I wanted to run the function with router.post and run the function (authDN) , So whenever POST call is made I wanted to show the response based on the response returned by authDN ,And I wanted to pass the userNT , password from the postData function to the authDN as well

Can someone help me to approach this

var express = require('express');
var router = express.Router();
var ldap = require('ldapjs');
var bodyParser = require('body-parser');
var userNT;
var password;


var app = express();
function authDN(dn, password, cb) {
    var client = ldap.createClient({ url: 'ldap://localhost:389' });
    client.bind(dn, password, function (err) {
        client.unbind();
        cb(err === null, err);
    });

}

function output(res, err) {
    if (res) {
        console.log('success');
    } else {
        console.log('failure');
    }
}



app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: false })); // support encoded bodies

router.post('/login', postData, authDN(userNT, password, output));

function postData(req, res) {
    userNT = req.body.ntid;
    password = req.body.password
};


module.exports = router;
Mahesh G
  • 1,226
  • 4
  • 30
  • 57

1 Answers1

1
router.post('/login', postData);

function postData(req, res) {
    userNT = req.body.ntid;
    password = req.body.password;
    authDN(userNT, password, output,res);   //send res also
};

function authDN(dn, password, cb,res) {
    var client = ldap.createClient({ url: 'ldap://localhost:389' });
    client.bind(dn, password, function (err) {
        client.unbind();
       cb(err === null, err,res); //pass res to callback
    });

}

function output(fake_res, err, res) {
    if (fake_res) {
       console.log('success');
       res.send('success')   //here
    } else {
       console.log('failure');
       res.send('failure')   //here
    }
}
wrangler
  • 3,454
  • 1
  • 19
  • 28
  • Can you please help me how to show the response of `authDN` as the response of POST call – Mahesh G Dec 28 '17 at 15:01
  • I tried `res.send(authDN(userNT, password, output));` But I am not getting any response in the POSTMAN after the POST call – Mahesh G Dec 28 '17 at 15:03
  • When I run `authDN` I will get the Output as either `success or failure` now I wanted to show the same response in the POST response to the users, I have tried `res.send(authDN(userNT, password, output));` but Its not showing anything – Mahesh G Dec 28 '17 at 15:10
  • Thanks a lot It worked!! , BTW if you have a time can you please let me know why we used `fake_res` Sorry I am noob in the `Node` – Mahesh G Dec 28 '17 at 15:22