小编Íca*_*ota的帖子

如何在节点中的函数调用中等待回调?

好的,所以我尝试使用加密生成公钥和私钥(https://nodejs.org/api/crypto.html#crypto_crypto_generatekeypair_type_options_callback

问题是,generateKeyPair 的参数之一是回调函数,我无法让我的代码等待回调运行。它最终会运行,但那时我已经尝试使用这些数据。我们的想法是做这样的事情:

const _getKeyPair = (pwd: string): Object => {
    const { generateKeyPair }: any = require('crypto');

    const keyPair = { publicKey: '', privateKey: '' };
    generateKeyPair('rsa', {
        modulusLength: 4096,
        publicKeyEncoding: {
            type: 'spki',
            format: 'pem'
        },
        privateKeyEncoding: {
            type: 'pkcs8',
            format: 'pem',
            cipher: 'aes-256-cbc',
            passphrase: pwd
        }
    }, (err: Error, publicKey: string, privateKey: string) => {
        if (err) {
            throw err;
        }
        keyPair.publicKey = publicKey;
        keyPair.privateKey = privateKey;
    });

    return keyPair;
};
Run Code Online (Sandbox Code Playgroud)

并致电:

const keyPair = …
Run Code Online (Sandbox Code Playgroud)

node.js async-await typescript

2
推荐指数
1
解决办法
1266
查看次数

如果数据库不存在,有没有办法通过sequelize 创建数据库?

我有续集连接。

然后,我创建我需要的每个表(如果它们尚不存在)。然而,在我这样做之前,如果模式不存在,我想创建它本身,这样我就可以在安装了 mysql 的任何地方运行这个脚本,而不必担心。有没有办法用续集来做到这一点?或者如果没有的话使用任何其他工具。

mysql node.js sequelize.js typescript

1
推荐指数
1
解决办法
4699
查看次数

标签 统计

node.js ×2

typescript ×2

async-await ×1

mysql ×1

sequelize.js ×1