How to implement query string parameters for RESTful APIs

Dawn17

I am new to RESTful APIs and I've successfully implemented the GET and DELETE command for my API (GET localhost:4000/api, DELETE localhost:4000/api on Postman works fine).

Code for my get looks like:

router.get('/', function(req, res) {
    user.find({}, function(err, users) {
        if(err){
            res.status(404).send({
                message: err,
                data: []
            });
        } else {
            res.status(200).send({
                message: 'OK',
                data: users
            });
        }
    });
});

Now I want to implement using parameters. For example, I want to implement something like sorting where

http://localhost/4000/api/users?sort={"name": 1} (1- ascending; -1 - descending)

would mean sorting the name in ascending order.

What I am not sure how to do is:

  1. How do I make the ?sort thing work?

  2. How do I select which field to sort?

Please help!

donquixote

You can only pass order(asc, desc), if you want to sort by name you can do like that http://localhost/4000/api/users?order=-1 or http://localhost/4000/api/users?&order=1

then in your controller

router.get('/', function(req, res) {
  let order = req.query.order;
  user
   .find({})
   .sort({"name": order}) 
   .exec(function(err, users) {
      if(err){
          res.status(404).send({
              message: err,
              data: []
          });
      } else {
          res.status(200).send({
              message: 'OK',
              data: users
          });
      }
  });

});

These works if you use mongoose.js for mongodb

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to implement query parameters in Postman with mongoose

分類Dev

How to get query params from protractor APIs

分類Dev

Limit amount of parameters in query string

分類Dev

Code Demand Constraint for RESTful APIs

分類Dev

How to implement continuous String loop

分類Dev

Regex for URL rewrite with optional query string parameters

分類Dev

Dynamic database requests without query string parameters

分類Dev

Converting a query parameters string into a regex expression

分類Dev

How can I get the right day of the week, if I am setting the date in relation to the given query string parameters?

分類Dev

How to implement "order by" together with "group by" in query

分類Dev

How to implement two parameters that are each optional with available values for each parameter?

分類Dev

How do I implement paging on a route with other parameters?

分類Dev

How do I get query parameters in the URL

分類Dev

How to add in dynamic parameters into a graphql query

分類Dev

How to pass parameters in a native query JPA

分類Dev

How to remove case insensitive query parameters with Apigee?

分類Dev

How to fix String field does not implement `Copy`?

分類Dev

REST API plugin - use body instead of query string for parameters

分類Dev

passing optional query string parameters to http service call

分類Dev

Getting parameters from query string in Zend Framework 2.3

分類Dev

How to Create query string in jquery?

分類Dev

How to create a string query in javascript?

分類Dev

PostgreSQL How to query String array

分類Dev

How do I pass modified string parameters?

分類Dev

How to implement ON ( ... OR ...) mysql query in doctrine2 queryBuilder

分類Dev

How to implement query schema on mongodb for ecommerce in C#

分類Dev

How to allow the URL to alter the query using multiple parameters?

分類Dev

How to show parameters which were passed into the query, on the report

分類Dev

Android Retrofit: URL query string "text={userInput}&key={apiKey}" must not have replace block. For dynamic query parameters use @Query

Related 関連記事

  1. 1

    How to implement query parameters in Postman with mongoose

  2. 2

    How to get query params from protractor APIs

  3. 3

    Limit amount of parameters in query string

  4. 4

    Code Demand Constraint for RESTful APIs

  5. 5

    How to implement continuous String loop

  6. 6

    Regex for URL rewrite with optional query string parameters

  7. 7

    Dynamic database requests without query string parameters

  8. 8

    Converting a query parameters string into a regex expression

  9. 9

    How can I get the right day of the week, if I am setting the date in relation to the given query string parameters?

  10. 10

    How to implement "order by" together with "group by" in query

  11. 11

    How to implement two parameters that are each optional with available values for each parameter?

  12. 12

    How do I implement paging on a route with other parameters?

  13. 13

    How do I get query parameters in the URL

  14. 14

    How to add in dynamic parameters into a graphql query

  15. 15

    How to pass parameters in a native query JPA

  16. 16

    How to remove case insensitive query parameters with Apigee?

  17. 17

    How to fix String field does not implement `Copy`?

  18. 18

    REST API plugin - use body instead of query string for parameters

  19. 19

    passing optional query string parameters to http service call

  20. 20

    Getting parameters from query string in Zend Framework 2.3

  21. 21

    How to Create query string in jquery?

  22. 22

    How to create a string query in javascript?

  23. 23

    PostgreSQL How to query String array

  24. 24

    How do I pass modified string parameters?

  25. 25

    How to implement ON ( ... OR ...) mysql query in doctrine2 queryBuilder

  26. 26

    How to implement query schema on mongodb for ecommerce in C#

  27. 27

    How to allow the URL to alter the query using multiple parameters?

  28. 28

    How to show parameters which were passed into the query, on the report

  29. 29

    Android Retrofit: URL query string "text={userInput}&key={apiKey}" must not have replace block. For dynamic query parameters use @Query

ホットタグ

アーカイブ