【问题标题】:Passing JSON data in Express Middleware在 Express 中间件中传递 JSON 数据
【发布时间】:2018-12-18 23:43:33
【问题描述】:

我的快递应用中有以下路线:

 app.post("/users/me/trackers/court_cases", caseValidator, DriversController.court_cases);

我希望能够将信息从我的第二个中间件 caseValidator 传递到第三组中间件。第二个中间件当前从 RESTful API 获取 JSON 数据,我想在将其发送给用户之前将其传递给最终路由。

这是我当前的案例验证器功能:

caseValidator = function(req, res, next){
    var case_id = req.body.case_id;

    var authOptions = {
        method: 'GET',
        url: `https://www.courtlistener.com/api/rest/v3/dockets/${case_id}/`,
        headers: {
            'Authorization' : "myauth"
        },
        json: true
    };

    var url = `https://www.courtlistener.com/api/rest/v3/dockets/${case_id}/`
    axios(authOptions)
        .then((response) => {
            console.log("success!")
            next();

            //// Pass in the JSON data to the next middleware?
        })
        .catch((error) => {
            res.status(400)
               .send(error)
        });
};

【问题讨论】:

    标签: node.js express server routing middleware


    【解决方案1】:

    你可以使用req.someVar

    axios(authOptions)
       .then(response => {
            console.log("success!");
            req.someVar = response.data;
            next();
       })
    

    然后在下一个中间件中您可以访问该数据。

    【讨论】:

      猜你喜欢
      • 2018-12-11
      • 2016-03-24
      • 1970-01-01
      • 2021-09-06
      • 2016-04-20
      • 1970-01-01
      • 1970-01-01
      • 2020-05-31
      • 1970-01-01
      相关资源
      最近更新 更多