如何使用随机string作为路由参数(ExpressJS)
我正在实现重置密码通过在URLpath中的随机string作为路由参数。 我后来在app.param中使用它。 当随机string包含字符“/”的应用程序无法正常工作。 以下是我的实施
在models / mymodelname.js中
resetId = crypto.randomBytes(16).toString('base64');
在路由/ mymodelname.js
app.post('/resetpassword/:resetId',users.resetPassword);
有什么办法可以使用我的resetId从随机string获取作为路由参数?
以下是几种可以解决此问题的方法:
-
使用
encodeURIComponent
函数将问题字符转换为它们的%XX
表示forms:resetId = crypto.randomBytes(16).toString('base64'); // ... resetIdEscaped = encodeURIComponent(resetId); // Example: L73jflJreR%2FuivSdnMU5%2Fg%3D%3D
-
将缓冲区转换为string时使用hex编码,而不是base64编码:
resetId = crypto.randomBytes(16).toString('hex'); // Example: 13e095f8967a1ba06d11eeeed616051d