Houshman85
Houshman85

Reputation: 72

Express Routing Params

I am looking to get this query from this url 'http://localhost:3000/#/quote/edit/?quote_number=1&line_number=1' from this route in express

router.route('/quote/:quote_number&:line_number')
.get(checkAuthentication, (req, res) => {
  let request = new sql.Request(pool)
  let get_lines_query = `
  select *
  from quote_line
  where quote_number = ${req.query.quote_number} and
  line_number = ${req.query.line_number}`

but it is calling a different query from

route router.route('/quote/:quote_number')

Upvotes: 1

Views: 365

Answers (2)

Taffarel Xavier
Taffarel Xavier

Reputation: 532

Try updating your code to:

router.route('/quote/:quote_number/:line_number').get(function (req, res) {
let quote_numer = req.params.quote_number;
let line_number= req.params.line_number;
  res.send(quote_numer  + "" + line_number)
})

Upvotes: 0

IceMetalPunk
IceMetalPunk

Reputation: 5566

Firstly, the URL you provided doesn't match the route. Your route is to /quote/number&line but your URL is of the form /quote/edit/?quote_number=number&line_number=line.

Secondly, parameters written as ?key=value&key2=value2 are called query parameters, and being special, standard forms of parameters, you don't need to bind them yourself. Express will parse them into the req.query object without you specifying the bindings. So all you need to do is change your route to /quote/edit and you're good.

On an unrelated note, please don't directly stick URL parameters into your SQL query. That's how you end up with SQL injection attacks. Depending on the SQL package you're using, there should be a way to use bound parameters in your queries instead.

Upvotes: 3

Related Questions