simple page with news from all around the globe
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

61 lines
2.0KB

  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. // The Express app is exported so that it can be used by serverless Functions.
  9. export function app(): express.Express {
  10. const server = express();
  11. const distFolder = join(process.cwd(), 'dist/news-demo/browser');
  12. const indexHtml = existsSync(join(distFolder, 'index.original.html')) ? 'index.original.html' : 'index';
  13. // Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
  14. server.engine('html', ngExpressEngine({
  15. bootstrap: AppServerModule,
  16. }));
  17. server.set('view engine', 'html');
  18. server.set('views', distFolder);
  19. // Example Express Rest API endpoints
  20. // server.get('/api/**', (req, res) => { });
  21. // Serve static files from /browser
  22. server.get('*.*', express.static(distFolder, {
  23. maxAge: '1y'
  24. }));
  25. // All regular routes use the Universal engine
  26. server.get('*', (req, res) => {
  27. res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] });
  28. });
  29. return server;
  30. }
  31. function run(): void {
  32. const port = process.env.PORT || 4000;
  33. // Start up the Node server
  34. const server = app();
  35. server.listen(port, () => {
  36. console.log(`Node Express server listening on http://localhost:${port}`);
  37. });
  38. }
  39. // Webpack will replace 'require' with '__webpack_require__'
  40. // '__non_webpack_require__' is a proxy to Node 'require'
  41. // The below code is to ensure that the server is run only when not requiring the bundle.
  42. declare const __non_webpack_require__: NodeRequire;
  43. const mainModule = __non_webpack_require__.main;
  44. const moduleFilename = mainModule && mainModule.filename || '';
  45. if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
  46. run();
  47. }
  48. export * from './src/main.server';