简体   繁体   English

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

[英]Nodejs - Express - Best practice to handle optional query-string parameters in API

I want to make a API which have 5 optional query parameters, I want to know if there is a better way to handle this, right now I check each one of them with if conditions, which is kind of dirty! 我想创建一个有5个可选查询参数的API,我想知道是否有更好的方法来处理它,现在我用if条件检查每一个,这有点脏! is there any way that I can handle all scenarios without using lot's of if conditions? 有没有什么方法可以处理所有场景而不使用if条件?

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
   })
}
.
.
.

I may find Lodash to be useful for this one: 我可能会发现Lodash对这个有用:

const response = songs.filter(song => {
   return _.isEqual(req.query, _.pick(song, Object.keys(req.query)))
})

I suggest you to use Joi 我建议你用Joi

It is very powerful library for javascript validations. 它是非常强大的javascript验证库。 You can make even conditional validations using it. 您甚至可以使用它进行条件验证。 See the complete docs . 查看完整的文档

I created basic schema for your scenario here. 我在这里为您的场景创建了基本架构。

// 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 });

It is easier to read and understand for other developers too. 对其他开发人员来说也更容易阅读和理解。 You can standardized the validations in the overall project. 您可以标准化整个项目中的验证。

You could use the ternary operator to do this all in one query. 您可以使用三元运算符在一个查询中执行此操作。 If the parameter is defined you check for equality and else you just return true. 如果定义了参数,则检查是否相等,否则只返回true。 This could look like this: 这看起来像这样:

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
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM