node.js - 弃用警告 : Buffer() is deprecated due to security and usability issues when I move my script to another server

标签 node.js npm

脚本移动到其他服务器时出错。

(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.

当前版本:

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) 更快,后者是您否则需要确保零​​填充。

关于node.js - 弃用警告 : Buffer() is deprecated due to security and usability issues when I move my script to another server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52165333/

相关文章:

node.js - `fs.createReadStream` 是否将默认结束值设置为文件的当前结尾?

javascript - 如何为每个端点和 REST 方法设置不同的 Passport JS Bearer 策略?

node.js - 使用 Node.js 和 Express 将非 www 重定向到 www

javascript - Node 控制台的 CoffeeScript 模式?

npm - 如何捕获 Karma 返回/退出代码?

node.js - npm、pnpm、yarn 或 pkglink 是否可以节省时间和磁盘空间?

node.js - amqplib : Socket closed abruptly during opening handshake

github - 如何直接从 GitHub 安装 npm 包

linux - $HOME/node_modules 中未安装 NodeJs npm WARN 卸载

npm - 无法使用 TSC - 读取文件时出错 "anything.ts": File not found