简体   繁体   中英

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?

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM