[英]How can i design two different REST API that one serves the list, and other one serves the details?
我遇到了需要設計REST API來解決我的CAR公司的兩個問題的情況。
1) Get the list of Cars.
2) Get the car Details.
所以,我決定這樣走。
1) GET /api/engine1/cars => Provies the list of cars.
2) GET /api/engine1/cars/detail (i would send the car number in the payload ).
請告訴我這是否是正確的方法?
您問題的答案以某種方式基於觀點,沒有一個正確的答案。
但是,有一些建議和成文的解決方案對其他人很有效。
請查看JSON API規范 。
可以做到的。 這是使用ExpressJS的示例:
const app = express();
app.use(parser.json());
app.use('/api/engine1/cars', <car>.router);
在汽車路由器文件中,您可以編寫如下內容:
router.route('/')
.get((req, res)=>{
res.json(<get the details for all cars here>);
});
router.route('/detail')
.get((req, res)=>{
const carPayload = req.body.payLoad;
res.json(<query details for all car based on payload number here>);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.