Node.js NodeJs和ExpressJs无法分离错误和响应处理中使用的公共代码

Node.js NodeJs和ExpressJs无法分离错误和响应处理中使用的公共代码,node.js,express,Node.js,Express,我不熟悉NodeJS和expressJs。我有以下代码,用于获取用户列表 在下面的代码中,我想将函数(错误、响应、主体)方法中的代码与不同方法中的重用分离 我怎么能做到?我在所有方法中重复使用这些,所以我想将其分离并在所有地方重用 router.get('/details/*******/all', function(req, res, next) { var cookie = request.cookie("JSESSIONID=" + req.cookies.JSESSIONID);

我不熟悉NodeJS和expressJs。我有以下代码,用于获取用户列表

在下面的代码中,我想将函数(错误、响应、主体)方法中的代码与不同方法中的重用分离

我怎么能做到?我在所有方法中重复使用这些,所以我想将其分离并在所有地方重用

router.get('/details/*******/all', function(req, res, next) {
    var cookie = request.cookie("JSESSIONID=" + req.cookies.JSESSIONID);
    jar.setCookie(cookie, LIST_OF_STAFF, function(error, cookie) {});
    request({
        url: LIST_OF_STAFF,
        method: "GET",
        jar: jar,
        headers: headers
    }, function(error, response, body) {
        if (!error && response.statusCode === 400 && !_.isUndefined(body)) {
            res.status(400)
            res.json(body);
        } else
        if (!error && response.statusCode === 401 && !_.isUndefined(body)) {
            res.status(401)
            res.json(body);
        } else
        if (!error && response.statusCode === 200 && !_.isUndefined(body)) {
            res.json(JSON.parse(body));
        } else {
            next(error);
        }
    });
});

你可以把函数移到另一个地方,给它起个名字。然后你可以重复使用它

以下是如何使用它的示例:

router.get('/details/*******/all', function(req, res, next) {
    var cookie = request.cookie("JSESSIONID=" + req.cookies.JSESSIONID);
    jar.setCookie(cookie, LIST_OF_STAFF, function(error, cookie) {});
    request({
        url: LIST_OF_STAFF,
        method: "GET",
        jar: jar,
        headers: headers
    }, newFunction.bind(null,res);
});

function newFunction (res,error, response, body) {
        // You can use res (res.write)
        if (!error && response.statusCode === 400 && !_.isUndefined(body)) {
            res.status(400)
            res.json(body);
        } else
        if (!error && response.statusCode === 401 && !_.isUndefined(body)) {
            res.status(401)
            res.json(body);
        } else
        if (!error && response.statusCode === 200 && !_.isUndefined(body)) {
            res.json(JSON.parse(body));
        } else {
            next(error);
        }
    }

你可以把函数移到另一个地方,给它起个名字。然后你可以重复使用它

以下是如何使用它的示例:

router.get('/details/*******/all', function(req, res, next) {
    var cookie = request.cookie("JSESSIONID=" + req.cookies.JSESSIONID);
    jar.setCookie(cookie, LIST_OF_STAFF, function(error, cookie) {});
    request({
        url: LIST_OF_STAFF,
        method: "GET",
        jar: jar,
        headers: headers
    }, newFunction.bind(null,res);
});

function newFunction (res,error, response, body) {
        // You can use res (res.write)
        if (!error && response.statusCode === 400 && !_.isUndefined(body)) {
            res.status(400)
            res.json(body);
        } else
        if (!error && response.statusCode === 401 && !_.isUndefined(body)) {
            res.status(401)
            res.json(body);
        } else
        if (!error && response.statusCode === 200 && !_.isUndefined(body)) {
            res.json(JSON.parse(body));
        } else {
            next(error);
        }
    }

感谢您的回复,您给出的解决方案不起作用,因为“res”对象在新函数中不可用,所以它会抛出错误,我会更新函数。现在,
res
可用。如果现在有帮助,请投票?或者接受回答谢谢你的回复,你给出的解决方案不起作用,因为“res”对象在新函数中不可用,所以它会抛出一个错误我会更新函数。现在,
res
可用。如果现在有帮助,请投票?还是接受答案