express.js를 사용하여 첫 번째 node.js 앱을 제작했습니다. 재미있었습니다 :) 나는 어떤 종류의 오해가있을 것입니다. 그래서 여기에갑니다.Express.js 미들웨어에 전달 된 매개 변수가 캐시됩니까?
app.all('/account/summary', apiPOST('AccountSummary', {FromDate: 'default', ToDate: 'default'}), function(req, res){
var data=req.apiJSON;
console.log(data);
res.render('accountsummary', {locals: data, layout: 'layouts/layout'});
});
apiPOST()과 같이 정의된다 :
apiPOST = function (operation, postParams) {
return function (req, res, next){
console.log('RIGHT AT START');
console.log(postParams);
console.log('END');
var currentDate = new Date();
var day = ('0'+currentDate.getDate()).slice(-2);
var month = ('0'+(currentDate.getMonth() + 1)).slice(-2);
var year = ('0'+currentDate.getFullYear()).slice(-4);
console.log('BEFORE DATES');
console.log(postParams);
if (typeof(postParams.FromDate)!='undefined' && (postParams.FromDate=='default' || postParams.FromDate=='')){
postParams.FromDate=year+'-'+month+'-'+day;
}
if (typeof(postParams.ToDate)!='undefined' && (postParams.ToDate=='default' || postParams.ToDate=='')){
postParams.ToDate=year+'-'+month+'-'+day;
}
//automatically add all posted data to postParams
if (typeof(req.body)!='undefined'){
for (var key in req.body){
if (req.body.hasOwnProperty(key)){
postParams[key]=req.body[key];
}
}
}
// here is do some talking to an XML web service and convert it to JSON;
// we use our postParams variable to POST
next();
}
}
우선이 잘 작동
나는 같은 정의 된 몇 가지 경로가 있습니다. GET 요청에서 페이지에 도달하면 기본적으로 FromDate 및 ToDate가 기본값으로 설정됩니다. 이 페이지에는 새로운 FromData 및 ToDate를 지정하기 위해 게시 할 수있는 양식이 있습니다. 게시 된 데이터가 자동으로 postParams에 추가되고 잘 작동합니다.
내가 겪고있는 문제는 다음에 사용자가 GET을 사용하여 페이지를 방문 할 때 이전에 게시 된 데이터가 여전히 주변에 있으므로 기본값이 아니라 현재로 설정된다는 것입니다.
왜 데이터를 사용할 수 있는지 알 수 없습니다. 외모로 보면, 게시되지 않고 오히려 postParams에 기억됩니다. postParams는 이제 전 세계에 있습니까?
감사합니다. 무슨 일
예, 맞습니다. 프로그램 시작시 라우트가 추가되고 다른 아파치 MVC 구현과 같은 HTTP 요청이 아니라는 것을 알아야합니다. 'postParams'는 해당 라우트 또는'apiPost'를 호출하는 모든 라우트에 대해 고유합니까? –
'apiPost'를 호출 할 때마다 자체 postParams 매개 변수가 생기므로 각 경로마다 고유하게 사용됩니다. – JohnnyHK
지금까지 도와 주셔서 감사합니다! 내 머리에 맞는지 궁금해. 함수를 반환한다는 사실은 경로에서 고유하게 유지되는 것입니까? 클로저의 전형적인 예입니까? –