使用node.js加载外部文件并与jwt.sign一起使用

使用node.js加载外部文件并与jwt.sign一起使用,node.js,jwt,node-modules,Node.js,Jwt,Node Modules,我正在尝试加载一个外部.pem文件,以便在node.js中使用JWT进行签名,但JWT.sign不会返回任何内容。 可能是.pem文件加载不正确 希望有人能帮忙 代码如下: function(properties, context) { const jwt = require('jsonwebtoken'); const https = require('https'); https.get(properties.privateKeyName, res =&

我正在尝试加载一个外部.pem文件,以便在node.js中使用JWT进行签名,但JWT.sign不会返回任何内容。 可能是.pem文件加载不正确

希望有人能帮忙

代码如下:

function(properties, context) {

    const jwt = require('jsonwebtoken');
    const https = require('https');


        https.get(properties.privateKeyName, res => {
            // Initialise an array
            const bufs = [];

            // Add the data to the buffer collection
            res.on('data', function (chunk) {
                bufs.push(chunk)
            });

            // This signifies the end of a request
            res.on('end', function () {
                // We can join all of the 'chunks' of the file together
                const privateKey = Buffer.concat(bufs);

                const issuer = properties.issuer;
                const client_id = properties.client_id;
                const aud = 'https://revolut.com'; // Constant
                const payload = {
                  "iss": issuer,
                  "sub": client_id,
                  "aud": aud
                };

                const token = jwt.sign(payload, privateKey, { algorithm: 'RS256', expiresIn: 60 * 60});

                return {
                    'jwt_token': token,
                    'jwt_test': 'test',
                }


            });
        })

}

我相信对于私钥算法来说不是RS256。它的RSA或ECDSA。

这是我从Revolution获得的文档:它的算法必须是RS256。@ReneMeldgaard你说得对。我的错。顺便说一句,您的pem文件是否托管在另一台服务器上?是的。这就是问题所在。我无法将其存储在本地。我建议您将缓冲区写入文件并验证该文件。好的,我可以让您分享一个示例吗?:)