Search code examples
node.jssslpem

where should we put key.pem and certificate.pem file for SSL?


I have written below code for SSL but i am getting error "SSL_ERROR_RX_RECORD_TOO_LONG " please let me know where to put key.pem and certificate.pem in my local to get rid of the error

if (settings.local) {
    https.createServer({
            key: fs.readFileSync('key.pem'),
            cert: fs.readFileSync('certificate.pem')

    }, app).listen(https_port);
    console.log("server starting on https://localhost:" + https_port);

Solution

  • This seems more like a TLS Problem for me. Don't worry, your code can read the certs, otherwise it would throw another Error.

    For your first question, I usually put them in certs/{cert.pem, key.pem}

    Then second:

    You probably tried to send non-SSL data to a server which excepts SSL. Try visiting your site via http, not https. http://localhost:port instead of https://localhost:port

    If that doesn't work either, I got some example code from my projects

    import https from "http2";
    import fs from "fs";
    import path from "path";
    import url from "url";
    
    const options:https.SecureServerOptions = {
        key: fs.readFileSync(path.join(__dirname, "certs/key.pem")),
        cert: fs.readFileSync(path.join(__dirname, "certs/cert.pem")),
        minVersion: "TLSv1.3" //Try 1.3 or 1.2
    };
    
    const server = https.createSecureServer(options, async (req, res) => {})
    .listen(https_port).on("listening", () => {
    console.log(`Server listening on port ${process.env.PORT || 3000}`);