最初,我尝试使用router.post
和router.get
方法,并在我的代码中将它们分开。
然后我选择使用router.all
,并在同一个函数中单独POST
和GET
并使用两个res.render
和一个共同对象与他们共享的viewData,因为它会加载相同的屏幕。
我想知道这是否是一个很好的方法,以及如何在node.js / express.js中完成:
router.all('/profile', function(req, res, next) {
var viewData = {
data1: Demo.getdata(1),
data2: Demo.getdata(2),
data3: Demo.getdata(3),
data4: Demo.getdata(4)
};
if (req.method === 'POST') {
request.get({
uri: res.locals.baseUrl + '/getData',
qs: {
param1: param1
}
}, function(error, response, body) {
if (error || response.statusCode != 200) {
viewData.message = 'There was a problem!!';
}
else{
var data = JSON.parse(body);
viewData.message = 'Good job!!';
viewData.moreData= data.moreData;
}
res.render('settings/profile', viewData);
});
}
else{
res.render('settings/profile', viewData);
}
});
答案 0 :(得分:1)
我建议您将它们分开,因为它们几乎没有任何共同点。
我要做的是避免复制/粘贴代码,SELECT CASE WHEN j.DocTypeID=0 AND CreditOrDebit = 'd' THEN SecDebit END AS 'debit',
CASE WHEN j.DocTypeID=0 AND CreditOrDebit = 'c' THEN SecCredit END AS 'credit',
docdate,
DATEDIFF(d,DocDate, GETDATE()) AS days,
CASE
WHEN DATEDIFF(d,DocDate, GETDATE()) <= 365 THEN
CASE WHEN j.DocTypeID=0 AND CreditOrDebit = 'c' THEN
-seccredit
ELSE
SecDebit
END
END AS 'secdebitlessthen365',
CASE
WHEN DATEDIFF(d, DocDate, GETDATE()) > 365 AND DATEDIFF(d, DocDate, GETDATE()) <= 730 THEN
CASE WHEN j.DocTypeID=0 AND CreditOrDebit = 'c' THEN
-seccredit
ELSE
SecDebit
END
END AS 'secdebitgreaterthen365lessthenorequa730',
CASE WHEN DATEDIFF(d, DocDate, GETDATE()) > 730 AND DATEDIFF(d, DocDate, GETDATE()) <= 1095 THEN
CASE WHEN j.DocTypeID=0 AND CreditOrDebit = 'c' THEN
-seccredit
ELSE
SecDebit
END
END AS 'secdebitgreaterthen730lessthenorequal1095',
CASE WHEN DATEDIFF(d, DocDate, GETDATE()) > 1095 THEN
CASE WHEN j.DocTypeID=0 AND CreditOrDebit = 'c' THEN
-seccredit
ELSE
SecDebit
END
END AS 'secdebitgreaterthen1095'
FROM J_Transactions (NOLOCK) j
WHERE AccountID IN (871446)
AND j.DocTypeID=0
调用router.post('/profile', ...)
,以便在需要时可以回退到res.redirect("/profile");
。