简体   繁体   English

如何使用随机字符串作为路由参数(ExpressJS)

[英]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. 我通过将url路径中的随机字符串作为路由参数来实现重置密码。 I later use it in app.param. 我稍后在app.param中使用它。 When the random string contains the character '/' app doesn't work properly. 当随机字符串包含字符“ /”时,应用无法正常运行。 Following is my implementation 以下是我的实现

in models/mymodelname.js 在models / mymodelname.js中

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

in routes/mymodelname.js 在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? 有什么方法可以使用从随机字符串中获取的resetId用作路由参数?

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: 使用encodeURIComponent函数将问题字符转换为其%XX表示形式:

     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: 缓冲区转换为字符串时,请使用十六进制编码而不是base64编码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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