こんにちは、私はノードを使い慣れていて、助けていただければ幸いです。Express Routing:ルータを通過したURLを取得するにはどうすればよいですか?
私はこれらの3つのルートを持っている:
router.get('/user/question', auth, ctrlQuestions.getUserQuestions);
router.get('/user/question/category/:category', auth, ctrlQuestions.getUserQuestionsByCategory);
router.get('/user/question/position/:position', auth, ctrlQuestions.getUserQuestionsByPosition);
は、彼らは基本的に1つのマングースコントローラのバリエーションです。最初の1は、第二のすべてQuestion
を取得し、第三行い、フィルタCategory
とPosition
I間違った3つの別々のコントローラではなく、私はちょうど1ルータにそれらを組み合わせることができれば、私は不思議でしたか?つまり、入力されたURLを確認する手段がquestion/category
またはquestion/position
である場合は、対応するreq.params
を取得しますか?
getUserQuestions
のためのコントローラは、次のようになります。
module.exports.getUserQuestions = function(req, res) {
if (req.payload.email) {
User
.findOne({"email" : req.payload.email})
.populate({
/////// There will be a match option here for filtering
path : 'questions',
})
.exec(function(err, user){
if (err) return sendJSONresponse(res, 404, err);
if (user.length == 0) return sendJSONresponse(res, 404, {"message" : "no user found"});
sendJSONresponse(res, 200, user.questions);
})
} else {
return sendJSONresponse(res, 404, {"message" : "invalid user email"})
}
}
あなたはまだ何を正確に一つのルータ上でそれらを配置しています質問ですか? – peteb