我是Node和Express的新手,其他一些與構建帶有節點的web應用程序一起工作的層,request
和response
參數讓我感到困惑。我的困惑在於,這兩個參數經常出現在一個函數中,但通常其中一個或兩個都沒有聲明。此外,大部分時間還會引入額外的參數,如'next'
或其他內容。例如,我有一個API以下路由器:節點和節點中間件中的'req'和'res'參數是什麼?
router.route('/teams')
// create a team at POST http://localhost:8080/api/teams
.post(function(req, res) {
var team = new Team();
team.name = req.body.name;
team.location = req.body.location;
// save the new team and check for errors
team.save(function(err) {
if (err) {
res.send(err);
};
res.json({ message: 'Team created!' });
});
})
// GET all the teams at GET http://localhost:8080/api/teams
.get(function(req, res) {
Team.find(function(err, teams){
if (err) {
res.send(err);
};
res.json(teams);
});
});
兩個.post
和.get
呼叫與req
和res
作爲參數的功能,但req
從未使用過。那麼函數如何知道如果req或res如果沒有被定義和使用,或者沒有以完全不同的順序使用,該如何處理?或者如果我給他們命名完全不同的東西?
請求和響應到底發生了什麼?對不起,我的無知。我已閱讀文檔,但不是點擊。
謝謝。
來到他們是從http://nodejs.org/的'request'和'response' API/http.html#http_event_request。 –