本文介绍了弃用警告:当我将脚本移动到另一台服务器时,由于安全和可用性问题,不推荐使用 Buffer()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
脚本移动到其他服务器时出错.
Getting error when script move to other server.
(节点:15707)[DEP0005] 弃用警告:由于安全和可用性问题,不推荐使用 Buffer().请改用 Buffer.alloc()、Buffer.allocUnsafe() 或 Buffer.from() 方法.
当前版本:
Ubuntu 16.04.4 LTS
Node - v10.9.0
NPM - 6.2.0
以前的版本:
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
注意 Buffer.alloc() 在当前 Node.js 版本上也比 new Buffer(size).fill(0) 更快,否则你需要这样做确保零填充.
Note that Buffer.alloc() is also faster on the current Node.js versions than new Buffer(size).fill(0), which is what you would otherwise need to ensure zero-filling.
这篇关于弃用警告:当我将脚本移动到另一台服务器时,由于安全和可用性问题,不推荐使用 Buffer()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!