【问题标题】:How to Run the Function with Express POST based the response from the Previous Middleware如何根据先前中间件的响应使用 Express POST 运行函数
【发布时间】:2018-06-09 02:56:41
【问题描述】:

我是 MEAN.io 的新手

下面的函数(authDN)编写&它在单独运行时运行良好,如下所示。

当我直接运行该函数时,我会在控制台中得到正确的响应

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

但我想用 router.post 运行该函数并运行该函数 (authDN) ,所以每当进行 POST 调用时,我想根据 authDN 返回的响应显示响应,我也想将userNTpasswordpostData函数传递给authDN

谁能帮我解决这个问题

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;

【问题讨论】:

    标签: node.js express mean-stack mean.io ldapjs


    【解决方案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
        }
    }
    

    【讨论】:

    • 你能帮我如何将authDN的响应显示为POST调用的响应
    • 我试过 res.send(authDN(userNT, password, output)); 但在 POST 调用后我没有在 POSTMAN 中得到任何响应
    • 当我运行 authDN 时,我将得到输出为 success or failure 现在我想在对用户的 POST 响应中显示相同的响应,我尝试了 res.send(authDN(userNT, password, output)); 但它没有显示任何东西
    • 非常感谢它成功了! ,顺便说一句,如果您有时间,请告诉我为什么我们使用fake_res 对不起,我是Node 中的菜鸟
    猜你喜欢
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 2016-01-03
    • 2014-07-06
    • 2020-08-14
    • 1970-01-01
    • 2022-08-15
    相关资源
    最近更新 更多