How to implement getting a get request for any number of different parameters?
Query string example:

/api/users/search?firstName=iv&lastName=cat /api/cars/search?color=red 
  • The question is not entirely clear ... - Vladimir Klykov
  • @ Vladimir Klykov For a gett query, a line is written in the handler - a template for which the request is parsed and the parameters are retrieved. For example router.get('/:instanceId', (req, res, next) => {}); . In this example, the instanceId parameter. My gap is that I need one such handler to handle a get request in which there can be any number of parameters with different names. - Max
  • var url = require('url'); var url_parts = url.parse(request.url, true); var query = url_parts.query; Code naguen per minute to enSO. - Vladimir Klykov
  • How to combine it with express? Can you give a more detailed answer? - Max

1 answer 1

To access query query parameters in express, there is a built-in request.query object that contains the key-value pairs of these parameters. For example, for a query like / api / users / search? FirstName = iv & lastName = cat, it will look like this:

 { firstName: 'iv', lastName: 'cat', } 

If the request is sent without parameters, then an empty object will be stored in request.query.

Here is a small example of using the request.query object to dynamically generate a database query:

 export const oldSetFilters = (filters: {[key: string]: string }): string => { let filtersString = ''; if ( filters && Object.keys(filters).length ) { for (const key in filters) { filtersString += `${ key }=${ filters[key] } AND `; } } filtersString = `WHERE ${filtersString.substring(0, filtersString.length - 5)}`; return filtersString; }; 

Handler route:

 app.route('/v2/:table') .get( async (req, res) => { try { const filters = oldSetFilters(req.query); const rows = await db.sqlGetRequest( `SELECT * FROM ${req.params.table} ${filters};` ); logger.info(`${req.params.table} were sent to admin`); res.status(200).send(rows); } catch (error) { logger.error(`${req.params.table} sending to admin failed`, error); res.status(500).send(error); } }) 

Hope this helps.

  • pancake, sawing out type declarations from the code, and then in the profile I saw that you were working with an angular and therefore you know typescript) - muturgan
  • Thanks for the answer, this is not exactly what I needed. I think I put it poorly when writing a question. I implemented the filtering logic itself, but I could not connect it with the request. I did not understand how to get the parameters from the request, then to transfer them for processing. The solution turned out to be elementary - in expresse, all parameters are parsed and placed in request.query. Source - Max
  • If I am not mistaken, your handler expects to receive a line different from those described in the question. Where do you get filters in the first example? in the second example, where does the table in the logger come from? - Max
  • About the table - of course there should be req.params.table. I'll fix it now. thank. about filters - from the custom function setFilters, which is described in the paragraph above and parses req.query - the same object with query parameters that you also found in the documentation yourself - muturgan
  • about the expected line - of course excellent. This is an abstract example. do you rewrite for your particular case or did you understand the principle? - muturgan 9:13 pm