raju
raju

Reputation: 4988

How to use random string as route parameter (ExpressJS)

I am implementing reset password by having the random string in the url path as a route parameter. I later use it in app.param. When the random string contains the character '/' app doesn't work properly. Following is my implementation

in models/mymodelname.js

resetId = crypto.randomBytes(16).toString('base64');

in routes/mymodelname.js

app.post('/resetpassword/:resetId',users.resetPassword);

Is there any way I can use my resetId got from random string to be used as route parameter?

Upvotes: 1

Views: 348

Answers (1)

user142162
user142162

Reputation:

Here are a couple of ways that you could solve this issue:

  1. Use the encodeURIComponent function to convert the problem characters into their %XX representation:

    resetId = crypto.randomBytes(16).toString('base64');
    // ...
    resetIdEscaped = encodeURIComponent(resetId);
    // Example: L73jflJreR%2FuivSdnMU5%2Fg%3D%3D
    
  2. Use hex encoding instead of base64 encoding when converting the buffer to a string:

    resetId = crypto.randomBytes(16).toString('hex');
    // Example: 13e095f8967a1ba06d11eeeed616051d
    

Upvotes: 1

Related Questions