Search code examples
javascriptnode.jsexpresslets-encryptgreenlock

Using greenlock-express with Express in Nodejs


I have an express app that was generated using express-generator and would like to use greenlock-express with it.

However unlike the example given by greenlock-express where you have

require('greenlock-express').create({ ... }).listen(80, 443);

the express app has .bin/www which was generated by express-generator where we have

var app = require('../app');
var http = require('http');

...

var server = http.createServer(app);
server.listen(port);

In this case, how should we use greenlock-express?


Solution

  • Just Update Your Greenlock Config

    For security reasons it'll probably be better to use the http/http2 (spdy) wrapper included with greenlock express as the server and require the app into greenlock, instead of trying to manage in directly.

    var greenlock = require('greenlock-express').create({
      ...
    , app: require('./app.js')
    });
    
    greenlock.listen(80, 443);
    

    When you want to run it on localhost, you can still use the server created by express-generator.

    Greenlock Part 2: Config for Express.js (Screencast)

    Just a few days ago I created a screencast showing exactly how to do this: https://www.youtube.com/watch?v=bTEn93gxY50&list=PLZaEVINf2Bq_lrS-OOzTUJB4q3HxarlXk