1

using nodejs / express, i'm trying to make a webservice using ? to separate url from parameters and then & as parameters separators.

when using this, it works

app.get("/tableref/:event/:queryObject", function(req, res) {})

or this one works also

app.get("/tableref:event&:queryObject", function(req, res) {})

but not this, I got 404 error:

app.get("/tableref?:event&:queryObject", function(req, res) {})

It seems that it is the ? that is the problem. Is there a way to authorize it? escape it?

I'd like to use express validator like this

Thanks

dt dino
  • 1,194
  • 6
  • 19
  • You seem to be confusing routing with standard GET parameters, which are accessed via `req.query.[PARAM_NAME]`. – Dave Newton Mar 25 '20 at 13:59
  • Does this answer your question? [How to parse variables in querystring using Express?](https://stackoverflow.com/questions/14669669/how-to-parse-variables-in-querystring-using-express) – imjared Mar 25 '20 at 13:59
  • I suggest you to take 5minutes to read this ; https://expressjs.com/en/4x/api.html#req – BENARD Patrick Mar 25 '20 at 14:00
  • it is because I want os use express-validator to validate params like this: https://tinaciousdesign.com/blog/express-route-param-validation-nodejs/ – dt dino Mar 25 '20 at 14:32

1 Answers1

0

To get value from query string you don't need to specify those query string parameters in Express routes.

Your code should look like this

app.get("/tableref", function(req, res) {
  res.json(req.query)
});

when you enter the URL localhost/tableref?hello&world you will get the response {"hello": "", "world": ""}

and if you want to pass data to query string variables, you can also enter the URL localhost/tableref?hello=world&foo=bar and you will get the response {"hello": "world", "foo": "bar"}

Pakpoom Tiwakornkit
  • 2,601
  • 1
  • 20
  • 19
  • doesn't seem to work. I still have the 404 error with the \ to escape – dt dino Mar 25 '20 at 14:22
  • @dtdino sorry for the previous code. It was my fault. Now my code is updated and I'm sure it solves your problem and works for you. Please test it again and tell me if it works now. – Pakpoom Tiwakornkit Mar 25 '20 at 15:02
  • Yep, it works and it is what I had before I wanted to change for implementing express-validator like this https://tinaciousdesign.com/blog/express-route-param-validation-nodejs/ . So it is not really what I want. – dt dino Mar 26 '20 at 09:45