简体   繁体   中英

How can i design two different REST API that one serves the list, and other one serves the details?

I got into a situation where i need to design REST API that solves two purposes for my CAR company.

1) Get the list of Cars.
2) Get the car Details. 

So, i decided to go like this.

 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 ). 

Kindly, let me know if this is the right approach?

The answer to your question is somehow opinion based and there is no single correct answer.
But there are some recommendations and documented solutions what worked out well for others.

Check out the JSON API Specification for instance.

This could be done. Here is an example using ExpressJS:

const app = express();
app.use(parser.json());
app.use('/api/engine1/cars', <car>.router);

Inside car router file you can write something like this:

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>);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM