Home > front end >  how do i use two parameter for app.get() in node.js
how do i use two parameter for app.get() in node.js

Time:11-19

I want to do something, i want to search between two dates using Mysql/Node.JS / Express.JS Here is what? I do not know how to pass 2 parameters to a Node.JS app with app.get()

For instance, i have this

app.get('/api/v1/listcustomers/:subscription_id',function(req,res){
    //Rest of SQL query goes here
})

Now this is for one parameter, it gets data using one parameter, I want to get dates using two parameters, How do I do it in Node.JS

using this app.get('/api/v1/trans_details/:date_from',function(req,res))

So by coding it should look like so:

app.get('/api/v1/trans_details/:date_from',function(req,res){
    rest of code goes here, to get data using 2 parameter
})

How do i get dates using 2 parameters?

CodePudding user response:

you can send the data in one single string but in the middle insert a special value (*%$) and after that in the code split the string by the special value that you use before to create an array with your values.

CodePudding user response:

You have different options, you can use query string like

app.get('/api/v1/trans_details',function(req,res){
    const { from, to } = req.query;
})

and you will call it

/api/v1/trans_details?from=2021-01-01&to=2021-12-31

or if you want it as sections of the url you can do, that way the value will be in req.params instead of req.query

app.get('/api/v1/trans_details/:from/:to',function(req,res){
    const { from, to } = req.params;
})

and you will call it

/api/v1/trans_details/2021-01-01/2021-12-31

the express with pattern matching the url and extract it as expected

  • Related