Created
August 13, 2020 17:32
-
-
Save jgcarmona-com/9668c9d64ea940b0d08af13a810c4b0a to your computer and use it in GitHub Desktop.
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
/*************************************************************************************************** | |
* Load `$localize` onto the global scope - used if i18n tags appear in Angular templates. | |
*/ | |
import { APP_BASE_HREF } from '@angular/common'; | |
import { LOCALE_ID } from '@angular/core'; | |
import '@angular/localize/init'; | |
import { ngExpressEngine } from '@nguniversal/express-engine'; | |
import * as express from 'express'; | |
import { existsSync } from 'fs'; | |
import { join } from 'path'; | |
import 'zone.js/dist/zone-node'; | |
import { environment } from './environments/environment'; | |
// THIS FIX MOST OF THE COMMON ISSUES WITH SSR: | |
let browserPath = 'logtrip/browser'; | |
const enDistFolder = join(process.cwd(), browserPath + '/en'); | |
// Emulate browser APIs | |
const domino = require('domino'); | |
const fs = require('fs'); | |
const templateA = fs.readFileSync(join(enDistFolder, 'index.html')).toString(); | |
const win = domino.createWindow(templateA); | |
console.log('win'); | |
win.Object = Object; | |
console.log('Object'); | |
win.Math = Math; | |
console.log('Math'); | |
global['window'] = win; | |
global['document'] = win.document; | |
global['Event'] = win.Event; | |
global['navigator'] = win.navigator; | |
console.log('declared Global Vars....'); | |
/** I need to avoid sorting this line */ | |
import { AppServerModule } from './main.server'; | |
export function app(lang: string) { | |
const server = express(); | |
const distFolder = join( | |
process.cwd(), | |
`logtrip/browser/${lang}` | |
); | |
const indexHtml = existsSync(join(distFolder, 'index.original.html')) | |
? 'index.original.html' | |
: 'index'; | |
// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine) | |
server.engine( | |
'html', | |
ngExpressEngine({ | |
bootstrap: AppServerModule, | |
providers: [ | |
{provide: LOCALE_ID, useValue: lang} | |
] | |
}) | |
); | |
server.set('view engine', 'html'); | |
server.set('views', distFolder); | |
// const apiProxy = proxy('/api', { target: 'http://localhost:3000' }); | |
// server.use('/api', apiProxy); | |
// Example Express Rest API endpoints | |
// server.get('/api/**', (req, res) => { }); | |
// Serve static files from /browser | |
server.get( | |
'*.*', | |
express.static(distFolder, { | |
maxAge: '1y', | |
}) | |
); | |
// All regular routes use the Universal engine | |
server.get('*', (req, res) => { | |
res.render(`${indexHtml}`, { | |
req, | |
providers: [{ provide: APP_BASE_HREF, useValue: `${req.baseUrl}` }], | |
}); | |
}); | |
return server; | |
} | |
function run() { | |
const port = process.env.PORT || 4200; | |
// Start up the Node server | |
const appEs = app('es'); | |
const appEn = app('en'); | |
const server = express(); | |
server.use('/en', appEn); | |
server.use('/es', appEs); | |
server.use('', appEs); | |
server.listen(port, () => { | |
console.log(`Node Express server listening on http://localhost:${port}`); | |
}); | |
} | |
// Webpack will replace 'require' with '__webpack_require__' | |
// '__non_webpack_require__' is a proxy to Node 'require' | |
// The below code is to ensure that the server is run only when not requiring the bundle. | |
declare const __non_webpack_require__: NodeRequire; | |
const mainModule = __non_webpack_require__.main; | |
const moduleFilename = mainModule && mainModule.filename || ''; | |
if (!environment.production && moduleFilename === __filename || moduleFilename.includes('iisnode')) { | |
// WILL BE EXECUTED ONLY IN NON PRODUCTION ENVIRONMENTS | |
run(); | |
} | |
export * from './main.server'; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment