A
A
arseniylebedev2019-02-23 17:29:54
Node.js
arseniylebedev, 2019-02-23 17:29:54

Writing to the file only occurs after the node js process is closed. How to fix?

const NodeRSA = require('node-rsa');
const fs = require('fs');

function makeRSAKeys(b = 1024) {
    const rsa_keys = new NodeRSA({b: b});

    try {
        fs.statSync('./rsa_keys/public.key');
        fs.statSync('./rsa_keys/private.key');
        rsa_keys.importKey(fs.readFileSync('./rsa_keys/public.key', 'utf8'));
        rsa_keys.importKey(fs.readFileSync('./rsa_keys/private.key', 'utf8'));
    } catch (e) {
        if (e.code === 'ENOENT') {
            fs.writeFileSync('./rsa_keys/public.key', rsa_keys.exportKey('pkcs8-public-pem'));
            fs.writeFileSync('./rsa_keys/private.key', rsa_keys.exportKey('pkcs8-private-pem'));
        } else {
            console.log(e);
        }
    }
}

makeRSAKeys();

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question