在 Express 中间件中传递 JSON 数据
Passing JSON data in Express Middleware
我的快递应用程序中有以下路线:
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)
});
};
你可以使用 req.someVar
.
axios(authOptions)
.then(response => {
console.log("success!");
req.someVar = response.data;
next();
})
然后在下一个中间件中您可以访问该数据。
我的快递应用程序中有以下路线:
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)
});
};
你可以使用 req.someVar
.
axios(authOptions)
.then(response => {
console.log("success!");
req.someVar = response.data;
next();
})
然后在下一个中间件中您可以访问该数据。