Skip to content

Instantly share code, notes, and snippets.

@peppertech
Created August 28, 2020 17:59
Show Gist options
  • Save peppertech/9d33336dbbb319f770e6ba864210e35f to your computer and use it in GitHub Desktop.
Save peppertech/9d33336dbbb319f770e6ba864210e35f to your computer and use it in GitHub Desktop.
before_serve hook with SSL and url-rewrite rules defined
/**
Copyright (c) 2015, 2020, Oracle and/or its affiliates.
Licensed under The Universal Permissive License (UPL), Version 1.0
as shown at https://oss.oracle.com/licenses/upl/
*/
"use strict";
module.exports = function (configObj) {
// Used for url-rewrite configuration
function toFilePath(url) {
const indexPath = "/index.html";
const matchStaticFiles = url.match(/\/(js|ts|css|ui)\/.*/);
return matchStaticFiles ? matchStaticFiles[0] : indexPath;
}
return new Promise((resolve, reject) => {
console.log("Running before_serve hook.");
const express = require("express");
/*
Setting up HTTPS for use with PWA
*/
const fs = require("fs");
const https = require("https");
const key = fs.readFileSync("./key.pem");
const cert = fs.readFileSync("./cert.pem");
const passphrase = "1234";
/*
Setting up path info and url-rewrite rules so
CoreRouter urlPathAdapter will work at development time
*/
const path = require("path");
const pathToWeb = path.join(__dirname, "../../web");
const app = express();
app.get("/*", (req, res) => {
res.sendFile(path.join(pathToWeb, toFilePath(req.url)));
});
/*
Pass our modified Express instance and the SSL enabled server
to the tooling configuration
*/
configObj['express'] = app;
configObj['urlPrefix'] = 'https';
configObj['server'] = https.createServer({
key: key,
cert: cert,
passphrase: passphrase
}, app);
const tinylr = require("tiny-lr");
const lrPort = "35729";
// We also need to setup the liveReload server to use SSL
configObj["liveReloadServer"] = tinylr({ lrPort, key, cert, passphrase });
resolve(configObj);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment