0
假設我有兩條路線:我的服務器上有「cars」和「people」兩個路徑。Node JS REST Api - 模型之間的關係
app.get('/cars', getAll)
app.get('/cars/:id', get)
app.get('/people', getAll)
一切都很好。現在,我必須在「人員」模型中修改方法「getAll」。在這種方法中,我需要通過ID獲取他們所擁有的汽車。我不能使用方法「得到」,因爲這種方法我有類似
res.status(200).send(car)
我需要返回車上,而不是HTTP響應。現在,我回來的承諾,所以我的路線看起來象下面這樣:
app.get('/cars/:id, (req, res) => {
//...
model.get(id).then(car => {
res.status(200).send(car)
})
})
,但我不知道這是否是一個很好的解決方案。另一種方法是從「人」模型中的方法「getAll」調用API,但更糟糕。從其他模型調用方法的最佳方式是什麼?其中,將取決於人所產生的汽車名單
app.get('/people/:id/cars', getPeoplesCars);
:
問候