使用多参数Node.js查询Postgres

使用多参数Node.js查询Postgres,node.js,postgresql,Node.js,Postgresql,我刚从Node.js开始,用Postgresdb构建一个Node.js服务器,我有一些CRUD操作的路径和方法: router.get('/products', productController.listAllProducts); 在Postman中,urllocalhost:3000/api/products工作正常。 现在我试图在查询中包括3个参数城市,地区和国家 router.get('/products', productController.findCityProducts); /

我刚从Node.js开始,用
Postgres
db构建一个
Node.js
服务器,我有一些CRUD操作的路径和方法:

router.get('/products', productController.listAllProducts);
在Postman中,url
localhost:3000/api/products
工作正常。 现在我试图在查询中包括3个参数
城市
地区
国家

router.get('/products', productController.findCityProducts); // correct route??
在Postman中,我添加了3个参数,因此url是
localhost:3000/api/products?city=Bologna®ion=Emilia Romagna&country=Italy
,但结果与
listallyproducts
方法相同。。而且没有满足查询的记录..所以我猜,
“/products”路由不能分配多个方法..并且正在调用
listallyproducts
。我将其注释掉,现在确实正在调用
findCityProduct`

如何启用
listAllProducts
findCityProduct
查询?
非常感谢。

正如您所发现的,不能使用不同的控制器函数(listAllProducts、findCityProducts)定义具有相同路径和HTTP方法(GET/products)的两条路由

这里可以采取的一种简单方法是根据URL中参数的存在,调整控制器函数,使其行为有所不同

另外,值得注意的是,在SQL查询中使用任何外部输入之前,都应该对其进行消毒,以避免注入。

router.get('/products', productController.listProducts);


exports.listProducts = async (req, res) => {

  const { city, region, country } = req.query
  let response

  if (city && region && country) {
    response = await db.query('SELECT * FROM products WHERE city = $1 AND region = $2 AND country = $3', [city,region,country]);
  } else {
    response = await db.query('SELECT * FROM products ORDER BY productName ASC'); 
  }

  if (response.rowCount > 0) {
     res.status(200).send(response.rows);
  } else {
     res.status(404).send({ message: "No products found" });
  }
};

谢谢,很高兴看到我昨天差点就拿到了。我尝试了一个类似
if(req.query.city!=null)
的检查,然后得到了类似
const city=req.query.city
的值。。因此,我可以声明这3个变量,而无需指定从特定查询参数获取的值。我对安全性非常陌生,但所有数据都将在调用节点API的云函数中进行加密/描述,并且对db的写入/读取受用户/密码保护,如
DATABASE\u URL=postgres://{db\u username}:{db_password}@{host}:{port}/{db_name}
存储在
.env
文件中。为了提高安全级别,还需要采取哪些谨慎步骤?在这种情况下,您必须验证和清理通过URL提供的输入。请看这个问题:您好,在示例中的代码中,您使用的是
参数化查询
对吗?如果是,SQLI不应该像它们那样工作泰特:
绝对正确!节点postgres中的参数化查询支持是一流的。所有转义都由postgresql server完成,以确保跨方言、编码等的正确行为。
很抱歉再次打扰您,但“我对正常查询(允许SQLI)
准备的语句有点困惑
(需要
pg promise
)和
参数化查询
(在
节点postgres
中完成,如果正确,将自动执行输入清理..)
exports.findCityProducts = async (req, res) => {
  const city = req.query.city;
  const region = req.query.region;
  const country = req.query.country;
  const response = await db.query('SELECT * FROM products WHERE city = $1 AND region = $2 AND country = $3', [city,region,country]);
  if (response.rowCount > 0) {
    res.status(200).send(response.rows);
  } else {
    res.status(404).send({
      message: 'Product not found'
    });
  }
};
router.get('/products', productController.listProducts);


exports.listProducts = async (req, res) => {

  const { city, region, country } = req.query
  let response

  if (city && region && country) {
    response = await db.query('SELECT * FROM products WHERE city = $1 AND region = $2 AND country = $3', [city,region,country]);
  } else {
    response = await db.query('SELECT * FROM products ORDER BY productName ASC'); 
  }

  if (response.rowCount > 0) {
     res.status(200).send(response.rows);
  } else {
     res.status(404).send({ message: "No products found" });
  }
};