node.js - DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server
问题描述
Getting error when script move to other server.
(node:15707) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
Current Versions:
Ubuntu 16.04.4 LTS
Node - v10.9.0
NPM - 6.2.0
Previous Version:
Ubuntu 14.04.3 LTS
NPM - 3.10.10
Node - v6.10.3
exports.basicAuthentication = function (req, res, next) {
console.log("basicAuthentication");
if (!req.headers.authorization) {
return res.status(401).send({
message: "Unauthorised access"
});
}
var auth = req.headers.authorization;
var baseAuth = auth.replace("Basic", "");
baseAuth = baseAuth.trim();
var userPasswordString = new Buffer(baseAuth, 'base64').toString('ascii');
var credentials = userPasswordString.split(':');
var username = credentials[0] !== undefined ? credentials[0] : '';
var password = credentials[1] !== undefined ? credentials[1] : '';
var userQuery = {mobilenumber: username, otp: password};
console.log(userQuery);
User.findOne(userQuery).exec(function (err, userinfo) {
if (err || !userinfo) {
return res.status(401).send({
message: "Unauthorised access"
});
} else {
req.user = userinfo;
next();
}
});
}
解决方案
new Buffer(number) // Old
Buffer.alloc(number) // New
new Buffer(string) // Old
Buffer.from(string) // New
new Buffer(string, encoding) // Old
Buffer.from(string, encoding) // New
new Buffer(...arguments) // Old
Buffer.from(...arguments) // New
请注意,当前 Node.js 版本上的 Buffer.alloc() 也比 new Buffer(size).fill(0) 更快,否则您需要确保零填充。
推荐阅读
- javascript - Firebase 按字段排序集合
- c# - 如何防止 C# 和 Selenium 退出代码 0
- android - Mockwebserver takeRequest() 方法无限执行
- java - 为什么 main() 方法允许声明异常?
- tensorflow - 使用自定义 oneDNN 库实现构建 TensorFlow 的步骤是什么?
- types - 如果大小未知,如何将闭包存储在联合中?
- ruby-on-rails - Ruby on Rails 6 Turbolinks Stripe 表单值不会保留在背面
- java - 无法解包客户端发送的请求。尽管我能够向他们的服务器发送成功的请求,但它一直在抱怨我的打包程序
- javascript - javascript:结束和开始日期控制
- r - 用R中的for循环计算平均值