Buffer() is deprecated due to security and usability issues

Solution for Buffer() is deprecated due to security and usability issues
is Given Below:

I want to run a Discord bot, but this code is using Buffer and it keeps producing an error for this code:

const app = express();

app.get("/", (req,res) => {
    if((new Buffer(req.query.key, "base64")).toString("utf-8") == key && req.query.message) {
        let message = (new Buffer(req.query.message, "base64")).toString("utf-8");
        onWebhookMessage(message);
    }
})

app.listen(3000);

module.exports = {getClient:function(){return client;}};

And this is the error:

(node:600332) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received undefined
    at Function.from (buffer.js:330:9)
    at new Buffer (buffer.js:286:17)
    at /root/bot/index.js:182:9
    at Layer.handle [as handle_request] (/root/bot/node_modules/express/lib/router/layer.js:95:5)
    at next (/root/bot/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/root/bot/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/root/bot/node_modules/express/lib/router/layer.js:95:5)
    at /root/bot/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/root/bot/node_modules/express/lib/router/index.js:335:12)
    at next (/root/bot/node_modules/express/lib/router/index.js:275:10)

I’ve changed new Buffer() to Buffer.from() and it’s not working. It produces this error:

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received undefined
    at Function.from (buffer.js:330:9)
    at /root/bot/index.js:182:17
    at Layer.handle [as handle_request] (/root/bot/node_modules/express/lib/router/layer.js:95:5)
    at next (/root/bot/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/root/bot/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/root/bot/node_modules/express/lib/router/layer.js:95:5)
    at /root/bot/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/root/bot/node_modules/express/lib/router/index.js:335:12)
    at next (/root/bot/node_modules/express/lib/router/index.js:275:10)
    at expressInit (/root/bot/node_modules/express/lib/middleware/init.js:40:5)

I’ve updated Node.js (NPM not Yarn) to the latest version and the buffers too but still the same. Does anyone know how to fix this? Thank you!

You’re not checking whether req.query.key is actually a string. Your error reports you’re doing Buffer.from(undefined), which would happen if req.query.key is undefined.

Assuming that key should be a string, you can simply compare it directly. Therefore, something like this should be fine for your use case:

if (req.query.key === key && typeof req.query.message === 'string') {

You don’t even need to use Buffer here anymore, since Buffer.from(str).toString('utf-8') === str, as utf8 is the default encoding.