Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2019
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * *** NOTE ON IMPORTING FROM ANGULAR AND NGUNIVERSAL IN THIS FILE ***
  3.  *
  4.  * If your application uses third-party dependencies, you'll need to
  5.  * either use Webpack or the Angular CLI's `bundleDependencies` feature
  6.  * in order to adequately package them for use on the server without a
  7.  * node_modules directory.
  8.  *
  9.  * However, due to the nature of the CLI's `bundleDependencies`, importing
  10.  * Angular in this file will create a different instance of Angular than
  11.  * the version in the compiled application code. This leads to unavoidable
  12.  * conflicts. Therefore, please do not explicitly import from @angular or
  13.  * @nguniversal in this file. You can export any needed resources
  14.  * from your application's main.server.ts file, as seen below with the
  15.  * import for `ngExpressEngine`.
  16.  */
  17.  
  18. import "zone.js/dist/zone-node";
  19.  
  20. import * as express from "express";
  21. import { join } from "path";
  22.  
  23. // Express server
  24. const app = express();
  25.  
  26. const PORT = process.env.PORT || 4000;
  27. const DIST_FOLDER = join(process.cwd(), "dist/browser");
  28.  
  29. // * NOTE :: leave this as require() since this file is built Dynamically from webpack
  30. const {
  31.     AppServerModuleNgFactory,
  32.     LAZY_MODULE_MAP,
  33.     ngExpressEngine,
  34.     provideModuleMap,
  35. } = require("./dist/server/main");
  36.  
  37. // Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
  38. app.engine(
  39.     "html",
  40.     ngExpressEngine({
  41.         bootstrap: AppServerModuleNgFactory,
  42.         providers: [provideModuleMap(LAZY_MODULE_MAP)],
  43.     }),
  44. );
  45.  
  46. app.set("view engine", "html");
  47. app.set("views", DIST_FOLDER);
  48.  
  49. // Example Express Rest API endpoints
  50. // app.get('/api/**', (req, res) => { });
  51. // Serve static files from /browser
  52. app.get(
  53.     "*.*",
  54.     express.static(DIST_FOLDER, {
  55.         maxAge: "1y",
  56.     }),
  57. );
  58.  
  59. // All regular routes use the Universal engine
  60. app.get("*", (req, res) => {
  61.     res.render("index", { req });
  62. });
  63.  
  64. // Start up the Node server
  65. app.listen(PORT, () => {
  66.     console.log(`Node Express server listening on http://localhost:${PORT}`);
  67. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement