title: Using letsencrypt with Node Express application
body: <p>I'm trying to make the switch to https with my application. I want to use Letsencrypt, but all tutorials I've seen online states that this requires a separate agent setup, to renew the certificate at constant intervals. I recently found greenlock-express which seems to bake the renewal process into the https wrapping of my express application, so I do not need to setup a separate server acting as an renewal agent (or have I misunderstood its purpose?).</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>// Express
  import * as http from 'http';
  import * as https from 'https';
  import * as e from 'express';
  const LEX = require('greenlock-express');
  
  /** Run this using MyServer.Initialize(process.argv) **/
  export class MyServer {
    public app: e.Express;
    private clientPath = path.join(__dirname, './public');
  
    static Initialize(args: string[]): Promise&lt;any&gt; {
      return new MyServer()
        .start(args)
        .catch((error: any) =&gt; console.error(error);
    }
  
    constructor() { }
  
    start(args: string[]): Promise&lt;https.Server&gt; {
      // Simplified the code here, in order to only display relevant info
      return Promise.resolve(this.createServer());
    }
  
    public createServer(): https.Server {
      this.app = e(); // Creates an express application
  
      // Setup routes and standard stuff, which I removed to keep this question simple
  
      // Setup base route to everything else
      this.app.get('/*', (req: e.Request, res: e.Response) =&gt; {
        res.sendFile(path.resolve(this.clientPath, 'index.html'));
      });
  </code></pre>
  
  <p>Now, if I return the following here, the app starts up fine and responds to <a href="http://localhost" rel="nofollow noreferrer">http://localhost</a>.</p>
  
  <pre><code>    return this.app.listen(80)
        .on('listening', () =&gt; console.log(`Serving on http://localhost/`));
  </code></pre>
  
  <p>But if I instead try to wrap the greenlock-express thingy around my app like this (this is from the example given at <a href="https://www.npmjs.com/package/greenlock-express" rel="nofollow noreferrer">https://www.npmjs.com/package/greenlock-express</a>, so I'm a bit disappointed that I could not get it to work out of the box): </p>
  
  <pre><code>    const lex = LEX.create({
        server: 'staging',
        email: 'my.email@mailprovider.com',
        agreeTos: true,
        configDir: 'cert/',
        approveDomains: ['mydomain.org']
      });
  
      // Force redirect to https
      http.createServer(lex.middleware(require('redirect-https')())).listen(80, function () {
        console.log('Listening for ACME http-01 challenges on', this.address());
      });
  
      return &lt;https.Server&gt; https.createServer(lex.httpsOptions, lex.middleware(this.app))
        .listen(443, () =&gt; console.log(`Serving on http://localhost/`));
    }
  }
  </code></pre>
  
  <p>Nothing works. I do get redirected from <a href="http://localhost" rel="nofollow noreferrer">http://localhost</a> to <a href="https://localhost" rel="nofollow noreferrer">https://localhost</a>, but after that, the browser gives up and states that <code>The site cannot be reached</code>.</p>
  
  <p>Why? Have I misunderstood something?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I am aware that letsencrypt might have issues with resolving localhost as <em>my</em> domain. I do have a domain for my application, I just don't want to deploy something I'm not 100% sure works. I want the application to be runnable and testable from localhost. That's what I thought the </p>
  
  <pre><code>server: 'staging'
  </code></pre>
  
  <p>part of the greenlock-express config was for.</p>