Skip to content
Advertisement

Handling complex query parameters Express.Js

I’m making REST APIS with Express.js I have the following express route:

/api/customer

I added multiple query params to the route like this:

JavaScript

In my controllers I handle all of these with If and there are so many cases I feel that this method would not scale, is there a better way of handling this ?

This is the code for my controller, I’m using Sequelize to query the database:

JavaScript

Advertisement

Answer

You could do something like this:

JavaScript

BTW, in your code, the functions getCustomerByFirstName, getCustomerByAddress and getCustomerByNameAddress are expecting to receive name and address as string parameter, but you are passing names and addresses array. This might lead to errors…

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement