Last active
November 25, 2019 02:43
-
-
Save warrendodsworth/d8d30dbecbcf9589fa97c2cda660aa25 to your computer and use it in GitHub Desktop.
Node Servers - https://riptutorial.com/node-js/example/21004/using-an-iis-virtual-directory-or-nested-application-via--appsettings-
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
<script> | |
// to dynamically set base url by reading the virtual servers path from the url | |
// doesn't work perfectly, causes double load as the first set of script calls fire to the base / url without the vd, then the second set go to the vd url (runtime.js etc) | |
let p = location.pathname.split('/').filter(x => x); | |
let baseHref = '/'; | |
if (!location.origin.includes('localhost:4200')) { | |
baseHref += p[0] + '/' + p[1] + '/' + p[2] + '/'; | |
} | |
console.log('baseHref', baseHref); | |
window['base-href'] = baseHref; | |
document.write(`<base href="${window['base-href']}" />`); | |
</script> |
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
const express = require('express'); | |
const path = require('path'); | |
const server = express(); | |
const port = process.env.PORT || 3000; | |
const rootPath = path.join(__dirname, 'dist'); | |
// set vd path | |
var virtualDirPath = '/vd/myapp/'; | |
// dist | |
server.use(virtualDirPath, express.static(path.join(__dirname, 'dist'))); | |
// node_modules | |
server.use(virtualDirPath + 'node_modules', express.static(path.join(__dirname, 'node_modules'))); | |
// Setup a route at the index of our app | |
server.get(virtualDirPath, (req, res) => { | |
return res.sendFile(path.resolve(__dirname, 'dist', 'index.html')); | |
}); | |
server.listen(port, () => { | |
console.log(`Listening on ${port}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment