Search code examples
node.jsexpresscertificatelets-encrypt

lets encrypt certificate only accepted for root path


I've installed a certificate using Let's Encrypt Certbot. The site is reachable and connection is secure.

Say the site is www.xxxyyy.xyz but any route in there www.xxxyyy.xyz/route jumps to non-secure connection, and the site closes. Any idea what could be the problem?

The way I linked the certificate on an express server is the following:

const fs = require('fs');
const https = require('https');
const http = require('http');
const express = require('express');
const app = require('./app');

var options = {
    key: fs.readFileSync('/etc/letsencrypt/live/twotravelers.xyz/privkey.pem'),
    cert: fs.readFileSync('/etc/letsencrypt/live/twotravelers.xyz/fullchain.pem'),
};

var portSecure = '443';
var port = '80';

var httpsServer = https.createServer(/*options,*/ app).listen(portSecure, function(){
  console.log("https at port" + portSecure);
});

var httpServer = http.createServer(app).listen(port, function(){
console.log('http at port'+port)
})

Any ideas?


Solution

  • Found out the problem. For reading paths I was using:

    fs.readFileSync('<relativePath>/file')
    
    

    When running the app on the server, I set up a service, and this changes the working directory to root /. We can either add the absolute path:

    fs.readFileSync('<absolutePath>/file')
    
    

    Or add a workingDirectory to the service file (which is what I did).

    That was messing the path to certificates and other directories.


    Probably the best option is to npm install dotenv and control the behavior with environmental variables.