当脚本移动到其他服务器时发生错误。
(node:15707) [DEP0005] DeprecationWarning: 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();
}
});
}
然后让我们给那些得到错误的人一个小例子。
根据需要,也可以使用Buffer.from(string)来代替Buffer.alloc(size)
使用Buffer.alloc(大小)
buf = Buffer.alloc(26);
for (var i = 0; i < 26; i++) {
buf[i] = i + 97;}
console.log(buf.toString('ascii'));
console.log(buf.toString('ascii'));
console.log(buf.toString('utf8'));
console.log(buf.toString(undefined));
使用Buffer.from(字符串)
var buffer1 = Buffer.from('Hello Word!!!!');
var buffer2 = Buffer.from('Hello nldflsd343');
其余的代码并不重要,但它确实显示了一些Buffer用法。
var buffer3 = Buffer.concat([buffer1, buffer2]);
console.log("buffer3 content: " + buffer3.toString());
var result = buffer1.compare(buffer2);
if (result < 0) {
console.log(buffer1 + "comes before " + buffer2);
} else if (result == 0) {
console.log(buffer1 + "is same as " + buffer2);
} else if (result > 0) {
console.log(buffer1 + "comes before " + buffer2);
}
希望这对你的生意有好处
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()也比新的Buffer(size).fill(0)更快,否则你需要它来确保零填充。
然后让我们给那些得到错误的人一个小例子。
根据需要,也可以使用Buffer.from(string)来代替Buffer.alloc(size)
使用Buffer.alloc(大小)
buf = Buffer.alloc(26);
for (var i = 0; i < 26; i++) {
buf[i] = i + 97;}
console.log(buf.toString('ascii'));
console.log(buf.toString('ascii'));
console.log(buf.toString('utf8'));
console.log(buf.toString(undefined));
使用Buffer.from(字符串)
var buffer1 = Buffer.from('Hello Word!!!!');
var buffer2 = Buffer.from('Hello nldflsd343');
其余的代码并不重要,但它确实显示了一些Buffer用法。
var buffer3 = Buffer.concat([buffer1, buffer2]);
console.log("buffer3 content: " + buffer3.toString());
var result = buffer1.compare(buffer2);
if (result < 0) {
console.log(buffer1 + "comes before " + buffer2);
} else if (result == 0) {
console.log(buffer1 + "is same as " + buffer2);
} else if (result > 0) {
console.log(buffer1 + "comes before " + buffer2);
}
希望这对你的生意有好处