server.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import 'zone.js/dist/zone-node';
  2. import { ngExpressEngine } from '@nguniversal/express-engine';
  3. import * as express from 'express';
  4. import { join } from 'path';
  5. import { AppServerModule } from './src/main.server';
  6. import { APP_BASE_HREF } from '@angular/common';
  7. import { existsSync } from 'fs';
  8. // Polyfills pour le SSR
  9. const domino = require('domino');
  10. const fs = require('fs');
  11. const path = require('path');
  12. // const distFolder = join(process.cwd(), 'dist/fatboar/browser');
  13. // const indexHtml = existsSync(join(distFolder, 'index.original.html')) ? 'index.original.html' : 'index';
  14. // const template = fs.readFileSync(join(distFolder, indexHtml)).toString();
  15. const distFolder = join(process.cwd(), 'dist/fatboar/browser');
  16. const indexHtml = existsSync(join(distFolder, 'index.html')) ? 'index.html' : 'index';
  17. const template = fs.readFileSync(join(distFolder, indexHtml)).toString();
  18. const win = domino.createWindow(template);
  19. global['window'] = win;
  20. global['document'] = win.document;
  21. global['navigator'] = win.navigator;
  22. global['CSS'] = null;
  23. // The Express app is exported so that it can be used by serverless Functions.
  24. export function app() {
  25. const server = express();
  26. // Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
  27. server.engine('html', ngExpressEngine({
  28. bootstrap: AppServerModule,
  29. }));
  30. server.set('view engine', 'html');
  31. server.set('views', distFolder);
  32. // Example Express Rest API endpoints
  33. // server.get('/api/**', (req, res) => { });
  34. // Serve static files from /browser
  35. server.get('*.*', express.static(distFolder, {
  36. maxAge: '1y'
  37. }));
  38. // All regular routes use the Universal engine
  39. server.get('*', (req, res) => {
  40. res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] });
  41. });
  42. return server;
  43. }
  44. function run() {
  45. const port = process.env.PORT || 4000;
  46. // Start up the Node server
  47. const server = app();
  48. server.listen(port, () => {
  49. console.log(`Node Express server listening on http://localhost:${port}`);
  50. });
  51. }
  52. // Webpack will replace 'require' with '__webpack_require__'
  53. // '__non_webpack_require__' is a proxy to Node 'require'
  54. // The below code is to ensure that the server is run only when not requiring the bundle.
  55. declare const __non_webpack_require__: NodeRequire;
  56. const mainModule = __non_webpack_require__.main;
  57. const moduleFilename = mainModule && mainModule.filename || '';
  58. if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
  59. run();
  60. }
  61. export * from './src/main.server';
  62. // import 'zone.js/dist/zone-node';
  63. // import { ngExpressEngine } from '@nguniversal/express-engine';
  64. // import * as express from 'express';
  65. // import { join } from 'path';
  66. // import { AppServerModule } from './src/main.server';
  67. // import { APP_BASE_HREF } from '@angular/common';
  68. // import { existsSync } from 'fs';
  69. // // The Express app is exported so that it can be used by serverless Functions.
  70. // export function app() {
  71. // const server = express();
  72. // const distFolder = join(process.cwd(), 'dist/fatboar/browser');
  73. // const indexHtml = existsSync(join(distFolder, 'index.original.html')) ? 'index.original.html' : 'index';
  74. // // Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
  75. // server.engine('html', ngExpressEngine({
  76. // bootstrap: AppServerModule,
  77. // }));
  78. // server.set('view engine', 'html');
  79. // server.set('views', distFolder);
  80. // // Example Express Rest API endpoints
  81. // // server.get('/api/**', (req, res) => { });
  82. // // Serve static files from /browser
  83. // server.get('*.*', express.static(distFolder, {
  84. // maxAge: '1y'
  85. // }));
  86. // // All regular routes use the Universal engine
  87. // server.get('*', (req, res) => {
  88. // res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] });
  89. // });
  90. // return server;
  91. // }
  92. // function run() {
  93. // const port = process.env.PORT || 4000;
  94. // // Start up the Node server
  95. // const server = app();
  96. // server.listen(port, () => {
  97. // console.log(`Node Express server listening on http://localhost:${port}`);
  98. // });
  99. // }
  100. // // Webpack will replace 'require' with '__webpack_require__'
  101. // // '__non_webpack_require__' is a proxy to Node 'require'
  102. // // The below code is to ensure that the server is run only when not requiring the bundle.
  103. // declare const __non_webpack_require__: NodeRequire;
  104. // const mainModule = __non_webpack_require__.main;
  105. // const moduleFilename = mainModule && mainModule.filename || '';
  106. // if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
  107. // run();
  108. // }
  109. // export * from './src/main.server';