【发布时间】:2017-12-08 07:01:25
【问题描述】:
如下所示,在我的 server.js 文件中,我有一个 /POST Info 请求,该请求在表单提交时被调用。
我开始对 app.post 和 express 路由之间的区别感到困惑,如果无论如何使用路由会对我的代码有好处。
在 /POST 信息中,我有两个对 2 个不同 API 的 axios 请求,我认为将代码移到其他地方以使其更清晰是明智的。
知道这里的路线如何运作对我有好处吗?如果你能解释一下这里的区别,那就太好了。
app.post('/Info', function (req, res) {
var State = req.body.State;
var income = Number(req.body.income);
var zip = req.body.ZIP;
axios.post('https://taxee.io/api/v2/calculate/2017', {
//data sent to Taxee.io
"exemptions": 1
, "filing_status": "single"
, "pay_periods": 1
, "pay_rate": income || 100000
, "state": State || "NY"
}, {
headers: {
'Authorization': "Bearer <API_KEY>"
//headers
}
}).then(function (response) {
var obj = {
income: '$' + income
, fica: response.data.annual.fica.amount
, federal: response.data.annual.federal.amount
, residence: State + ", " + zip
, state: response.data.annual.state.amount
}
axios.get("https://www.quandl.com/api/v3/datasets/ZILL/Z" + zip + "_RMP.json?api_key=<API_KEY>").then(function (response) {
var monthRent = response.data.dataset.data[0][1]
obj.rent = monthRent
obj.yearlyRent = Number(monthRent) * 12;
}).then(function (response) {
res.send(obj);
});
}).catch(function (error) {
alert('error');
});
}
【问题讨论】:
标签: node.js express routes axios