theosp
theosp

Reputation: 7549

How to run docpad over ssl?

I want to let users to access all the site's pages using both http and https.

I also want to be able to redirect http requests for certain paths (/api/* and /backend/*) that require encryption to https (force https).

Upvotes: 2

Views: 154

Answers (1)

theosp
theosp

Reputation: 7549

node version: v0.9.10

docpad version: v6.21.10

Important note: It seems that the documentation for the docpad config file is outdated, I had to dig the source to find that the serverHttp and serverExpress options now have to be under the server option.

here's the project's docpad.js file:

var https = require('https'),
path = require('path'),
fs = require('fs'),
express = require('express');

var sslOptions = {
                  key: fs.readFileSync(path.resolve(__dirname, "../../certificates/key.pem")),
                  cert: fs.readFileSync(path.resolve(__dirname, "../../certificates/cert.pem"))
              };

serverExpress = express();
serverHttps = https.createServer(sslOptions, serverExpress);

docpadConfig = {
    environments: {
        http: {
            port: 80,
            events: {
                serverExtend: function (server) {
                    var i = 0;

                    // Redirect requests that requires https
                    server.server.get(/^\/(api|backend)\/.*/, function (req, res) {
                        res.redirect('https://' + req.headers.host + req.url)
                    });
                }
            }
        },
        https: {
            port: 443,
            server: {
                serverHttp: serverHttps,
                serverExpress: serverExpress
            }
        }
    }
};

module.exports = docpadConfig;

Run the two environments with:

docpad --env "http" run
docpad --env "https" run

and that's it.

Upvotes: 2

Related Questions