代码之家  ›  专栏  ›  技术社区  ›  Emad Dehnavi

Nodejs-Express-API中处理可选查询字符串参数的最佳实践

  •  4
  • Emad Dehnavi  · 技术社区  · 7 年前

    let songName = req.query.songName
    let singerName = req.query.singerName
    let albumName = req.query.albumName
    let publishDate = req.query.publishDate
    
    if(songName && singerName && albumName && publishDate) {
       const response = songs.filter(c => { 
          return c.songName === songName && c.singerName === singerName && c.albumName === albumName && c.publishDate === publishDate
       }
       res.send({
          "Data" : response
       })
    }
    
    if(songName && singerName && albumName && !publishDate) {
       const response = songs.filter(c => { 
          return c.songName === songName && c.singerName === singerName && c.albumName === albumName
       }
       res.send({
          "Data" : response
       })
    }
    
    if(songName && singerName && !albumName && publishDate) {
       const response = songs.filter(c => { 
          return c.songName === songName && c.singerName === singerName && c.publishDate === publishDate
       }
       res.send({
          "Data" : response
       })
    }
    
    if(songName && !singerName && albumName && publishDate) {
       const response = songs.filter(c => { 
          return c.songName === songName && c.albumName === albumName && c.publishDate === publishDate
       }
       res.send({
          "Data" : response
       })
    }
    
    if(!songName && singerName && albumName && publishDate) {
       const response = songs.filter(c => { 
          return c.singerName === singerName && c.albumName === albumName && c.publishDate === publishDate
       }
       res.send({
          "Data" : response
       })
    }
    .
    .
    .
    
    3 回复  |  直到 7 年前
        1
  •  2
  •   Steffen Schmitz    7 年前

    您可以使用三元运算符在一个查询中完成此操作。如果定义了参数,则检查是否相等,否则只返回true。可能是这样的:

    const response = songs.filter(c => {
        return (songName ? (c.songName === songName) : true) &&
               (singerName ? (c.singerName === singerName) : true) &&
               (albumName ? (c.albumName === albumName) : true);
    });
    
    res.send({
        "Data": response
    })
    
        2
  •  2
  •   Anton Harniakou    7 年前

    我可能会发现Lodash对这个很有用:

    const response = songs.filter(song => {
       return _.isEqual(req.query, _.pick(song, Object.keys(req.query)))
    })
    
        3
  •  2
  •   Shaharyar    7 年前

    Joi

    它是一个非常强大的javascript验证库。您甚至可以使用它进行条件验证。见 complete docs .

    我在这里为您的场景创建了基本模式。

    // validation
    const schema = Joi.object().keys({
        songName: Joi.string()
        singerName: Joi.string()
        albumName: Joi.string()
        publishDate: Joi.date()
    });
    
    const { error, value } = Joi.validate(req.query, schema, { abortEarly: false, allowUnknown: false });
    if (error !== null) return res.send(400, { code: 400, message: "validation error", error: error.details });
    

    对于其他开发人员来说,它也更容易阅读和理解。您可以将整个项目中的验证标准化。