如何创建一个单一的API来获取所有用户和获取单个用户?

huangapple go评论100阅读模式
英文:

How to create single API for getallUsers and getUser?

问题

To create a single API that can handle both getting all users and a single user, you can use a route like this:

  1. router.get('/:id?', async (req, res) => {
  2. if (req.params.id) {
  3. // Get a single user by ID
  4. const user = await User.findById(req.params.id).select('-password');
  5. if (!user) {
  6. res.status(404).json({ message: 'User not found' });
  7. } else {
  8. res.status(200).json(user);
  9. }
  10. } else {
  11. // Get all users
  12. const userList = await User.find().select('-password');
  13. res.status(200).json(userList);
  14. }
  15. });

This route handles requests with or without an id parameter. If an id is provided, it retrieves a single user by ID. If not, it retrieves all users.

英文:

I have to create a single API to get all users and a single user. How can I create an API for this?

Currently, I had APIs like this.

To get All Users:

  1. router.get(`/`, async (req, res) =>{
  2. const userList = await User.find().select('-password')
  3. if(!userList) {
  4. res.status(500).json({success: false})
  5. }
  6. res.send(userList);
  7. })

To get single User:

  1. router.get('/:id' , async (req,res) =>{
  2. const user = await User.findById(req.params.id).select('-password')
  3. if(!user){
  4. res.status(500).json({message: 'The User with give id is not found'})
  5. }
  6. res.status(200).send(user)
  7. })

Now my requirement is that have to create a single API for above both APIs.

How can I solve this?

答案1

得分: 1

你可以告诉客户,如果他们需要所有用户,他们应该将"id"参数设为"all"。

  1. router.get("/:id", async (req, res) => {
  2. if (req.params.id === "all") {
  3. const userList = await User.find().select("-password");
  4. if (!userList) {
  5. res.status(500).json({ success: false });
  6. }
  7. res.send(userList);
  8. } else {
  9. const user = await User.findById(req.params.id).select("-password");
  10. if (!user) {
  11. res.status(500).json({ message: "未找到给定id的用户" });
  12. }
  13. res.status(200).send(user);
  14. }
  15. });
英文:

You can say clients that they should send all as id if they require all the users.

  1. router.get("/:id", async (req, res) => {
  2. if (req.params.id === "all") {
  3. const userList = await User.find().select("-password");
  4. if (!userList) {
  5. res.status(500).json({ success: false });
  6. }
  7. res.send(userList);
  8. } else {
  9. const user = await User.findById(req.params.id).select("-password");
  10. if (!user) {
  11. res.status(500).json({ message: "The User with give id is not found" });
  12. }
  13. res.status(200).send(user);
  14. }
  15. });

答案2

得分: 0

使用路由器

  1. let router = express.Router();
  2. router.get('/:id', function (req, res, next) {
  3. console.log("Specific User Router Working");
  4. res.end();
  5. });
  6. router.get('/all', function (req, res, next) {
  7. console.log("All User Router Working");
  8. res.end();
  9. });
  10. app.use('/user', router);
英文:

Use a router

  1. let router = express.Router();
  2. router.get('/:id', function (req, res, next) {
  3. console.log("Specific User Router Working");
  4. res.end();
  5. });
  6. router.get('/all', function (req, res, next) {
  7. console.log("All User Router Working");
  8. res.end();
  9. });
  10. app.use('/user',router);

huangapple
  • 本文由 发表于 2023年1月4日 19:52:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75005047.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定