node.jsexpressroutesrouteparams

How to create an api in express with route param and query param


My application is in nodejs with express.

Im trying to construct an API which has route param and query param both

below are the things what i have tried

 using **=**

 app.get('/:accountId/accounts/password/update?uniqueCode={uniqueCode}', async function(req, res) {
         //my code here
    }

and

app.get('/:accountId/accounts/password/update?uniqueCode/:uniqueCode', async function(req, res) {
             //my code here
   }

but when I hit this from my postman like below

http://localhost:5000/722/account/password/update?uniqueCode={dfgsfksjfksdhfksj}

I'm getting NOTFOUND error from express in both the ways that I have tried. Can anyone suggest how I can do it.


Solution

  • You've to check the queryParams inside your code :

    app.get('/:accountId/accounts/password/update', async function(req, res, next) {
              const accountId = req.params.accoundId;
              const  uniqueCode = req.query.uniqueCode;
             ...
              if (/* checkuniqueCode is not valid */) {
                   return next()
               }
    
     }
    

    Here is the doc : https://expressjs.com/fr/api.html#req.query