如何使用 nodejs 和 express 在 REST API 中实现搜索和过滤

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26108594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:55:56  来源:igfitidea点击:

How to implement search and filtering in a REST API with nodejs and express

node.jsapirestexpress

提问by dukable

I am learning and playing around with Node and Express by building a REST API. I don't have any DB to store data, I do everything in-memory.

我正在通过构建 REST API 来学习和使用 Node 和 Express。我没有任何数据库来存储数据,我在内存中做所有事情。

Let's say I have an array of users:

假设我有一组用户:

var users = [{"id": "1", "firstName": "John", "lastName": "Doe"}];

and defined a getAllUser function:

并定义了一个 getAllUser 函数:

exports.getAllUser = function(page, items) {
  page = (page < 1 ? 1 : page) || 1;
  items = (items < 1 ? 5 : items) || 5;
  var indexStart, indexEnd;
  indexStart = (page - 1) * items;
  indexEnd = indexStart + items;
  return users.slice(indexStart, indexEnd);
};

and defined a route:

并定义了一条路线:

router.get('/users', function(req, res, next) {
  var page = req.query.page;
      items = req.query.items;
  page = page !== 'undefined' ? parseInt(page, 10) : undefined;
  items = items !== 'undefined' ? parseInt(items, 10) : undefined;

  res.status(200).json({ users: users.search(page, items) });
});

All of this works fine, I have been able to test it with Postman and my data is being returned.

所有这些工作正常,我已经能够用 Postman 测试它并且我的数据正在返回。

My question is, how to implement search and filtering?

我的问题是,如何实现搜索和过滤?

From what I understand, search parameters will be passed in the URL as parameters, for example:

据我了解,搜索参数会在 URL 中作为参数传递,例如:

http://localhost:8080/api/users/firstName=john&age=30

How would I extract those parameters with node, and is there a specific lib to use or best practices to follow?

我将如何使用 node 提取这些参数,是否有特定的库可供使用或遵循最佳实践?

Same question for filtering, or is filtering the same thing than search?

相同的过滤问题,还是过滤与搜索相同?

回答by Jordonias

The parameters will be in req.query.

参数将在req.query.

{ 'firstName': 'john', 'age': '30' }

You can use arr.filter(callback[, thisArg])for filtering.

您可以arr.filter(callback[, thisArg])用于过滤。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

Something like this:

像这样的东西:

function search(query) {
  return function(element) {
    for(var i in query) {
      if(query[i] != element[i]) {
        return false;
      }
    }
    return true;
  }
}

exports.search = function(query) {
  return users.filter(search(query));
}

And in your route:

在你的路线中:

router.get('/users', function(req, res, next) {
  return res.json({ users: users.search(req.query) });
});

Note: In the searchfunction you may need to do something about case, type, etc.

注意:在search函数中,您可能需要对大小写、类型等进行处理。