2017-07-04 149 views
1

正如你可以在下面看到的,在我的server.js文件中,我有一個/ POST信息請求被調用表單提交。Axios請求內/快速POST請求

我開始對閱讀app.post和express路線之間的差異感到困惑,如果無論如何使用路線會使我的代碼在這裏受益。

在/ POST信息中我對兩個不同的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'); 
    }); 
} 

回答

1

有兩種方式在一個Express應用程序定義路線:

使用Express應用程序(app)對象直接:

const express = require('express') 
const app = express() 

app.post(...) 
app.get(...) 
app.put(...) 
// and so on 

或者使用router對象:

const express = require('express') 
const app = express() 
const router = express.Router() 

router.post(...) 
router.get(...) 
router.put(...) 
// and so on 

app.use(router) 

我的猜測是你一直在閱讀有關t他後面的代碼片段與router對象。使用Express'Router對象確實可以使代碼更清晰,因爲存在更多關注點分離。

從您自己的API調用外部API沒有任何問題。例如,在我的一個項目中,我致電this行上的Google Calendar API。我的唯一區別是你在使用標準HTTP請求時使用了Google APIs Node.js Client。我可以肯定地使用HTTP請求,如here所示。

您的代碼很好,但可以改進。例如,而不是:

axios.post('...', { 
    exemptions: 1, 
    filing_status: 'single', 
    pay_periods: 1, 
    pay_rate: income || 100000, 
    state: State || 'NY' 
}) 

你可以調用備選項對象的輔助功能:

function prepareOptions (state = 'NY', income = 100000) { 
    return { 
    exemptions: 1, 
    filing_status: 'single', 
    pay_periods: 1, 
    pay_rate: income, 
    state: State 
    } 
} 

然後調用它像這樣:

axios.post('...', prepareOptions(State, income)) 

這使得更具可讀性碼。

最後,沒有理由在服務器端使用axios。只需使用內置的HTTP module即可。

+0

http.get({ 主機名: '本地主機', 端口:80, 路徑: '/', 劑:假//創建一個新的代理只爲這一個請求 },(RES)=> { //做任何事情與迴應 });這種沒有代理的東西? – Aaron

1
app.post('/Info', function (req, res) { 

     var uData ={ 
      state: req.body.State, 
      income : Number(req.body.income), 
      zip: req.body.ZIP 
     }; 

     taxee(uData).then(function(data){ 

      return rent(data) ; 
     }).then(function(fullData){ 

      res.send(fullData); 
     }).catch(function (error) { 
     res.render('error'); 
    }); 
function taxee(data) { 
    return new Promise((resolve, reject) => { 

     var income = data.income; 
     var state = data.state; 
     var zip = data.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" 
     , }, header).then(function (response) { 
      var taxData = { 
       income: '$' + income 
       , fica: response.data.annual.fica.amount 
       , federal: response.data.annual.federal.amount 
       , stateTax: response.data.annual.state.amount 
       , state 
       , zip: zip 
      } 
      resolve(taxData); 
     }).catch(function (error) { 
      console.log('break'); 
      resolve(error); 
     }); 
    }); 
}; 

function rent(data) { 
    return new Promise((resolve, reject) => { 
     axios.get("https://www.quandl.com/api/v3/datasets/ZILL/Z" + data.zip + "_RMP.json?api_key=d7xQahcKCtWUC4CM1LVd").then(function (response) { 
      console.log(response.status, ' status'); 
      var monthRent = response.data.dataset.data[0][1]; 
      data.rent = monthRent 
      data.yearlyRent = Number(monthRent) * 12; 
      return data; 
     }).then(function (response) { 
      resolve(data); 
     }).catch(function (error) { 
      reject(error); 
     }); 
    }); 
} 
module.exports = { 
    taxee 
    , rent 
};  

結束了將上面的代碼放入clean promise方法。真的很高興它是如何解決的!