Created
August 28, 2020 17:59
-
-
Save peppertech/9d33336dbbb319f770e6ba864210e35f to your computer and use it in GitHub Desktop.
before_serve hook with SSL and url-rewrite rules defined
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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