|
1 |
| -import { environment } from './src/environments/environment'; |
2 |
| -import 'zone.js/dist/zone-node'; |
3 |
| - |
4 |
| -import { ngExpressEngine } from '@nguniversal/express-engine'; |
5 |
| -import * as express from 'express'; |
6 |
| -import { join } from 'path'; |
7 |
| - |
8 |
| -import { existsSync } from 'fs'; |
9 |
| - |
10 |
| -import { ISRHandler } from 'ngx-isr/server'; |
11 |
| -import { RedisCacheHandler } from './redis-cache-handler'; |
12 |
| - |
13 |
| -import bootstrap from './src/main.server'; |
14 |
| - |
15 |
| -// The Express app is exported so that it can be used by serverless Functions. |
16 |
| -export function app(): express.Express { |
17 |
| - const server = express(); |
18 |
| - const distFolder = join(process.cwd(), 'dist/docs/browser'); |
19 |
| - const indexHtml = existsSync(join(distFolder, 'index.original.html')) |
20 |
| - ? 'index.original.html' |
21 |
| - : 'index'; |
22 |
| - |
23 |
| - const REDIS_CONNECTION_STRING = process.env['REDIS_CONNECTION_STRING'] || ''; |
24 |
| - const INVALIDATE_TOKEN = process.env['INVALIDATE_TOKEN'] || ''; |
25 |
| - |
26 |
| - // Step 0 (optional): Create FileSystemCacheHandler with required options. |
27 |
| - // const fsCacheHandler = new FileSystemCacheHandler({ |
28 |
| - // cacheFolderPath: join(distFolder, '/cache'), |
29 |
| - // prerenderedPagesPath: distFolder, |
30 |
| - // addPrerenderedPagesToCache: true, |
31 |
| - // }); |
32 |
| - |
33 |
| - const redisCacheHandler = REDIS_CONNECTION_STRING |
34 |
| - ? new RedisCacheHandler({ connectionString: REDIS_CONNECTION_STRING }) |
35 |
| - : undefined; |
36 |
| - |
37 |
| - // Step 1: Initialize ISRHandler |
38 |
| - const isr = new ISRHandler({ |
39 |
| - indexHtml, |
40 |
| - cache: redisCacheHandler, // we can remove this field if we want to use the default InMemoryCacheHandler |
41 |
| - invalidateSecretToken: INVALIDATE_TOKEN || 'MY_TOKEN', |
42 |
| - enableLogging: !environment.production, |
43 |
| - buildId: environment.buildTimestamp + '', |
44 |
| - }); |
45 |
| - |
46 |
| - // Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine) |
47 |
| - server.engine('html', ngExpressEngine({ bootstrap })); |
48 |
| - |
49 |
| - server.set('view engine', 'html'); |
50 |
| - server.set('views', distFolder); |
51 |
| - |
52 |
| - // needed for post requests in our case we use it for the invalidation url |
53 |
| - server.use(express.json()); |
54 |
| - |
55 |
| - // Example Express Rest API endpoints |
56 |
| - // server.get('/api/**', (req, res) => { }); |
57 |
| - |
58 |
| - // Step 2: Add invalidation url handler |
59 |
| - server.post('/api/invalidate', async (req, res) => await isr.invalidate(req, res)); |
60 |
| - |
61 |
| - // Serve static files from /browser |
62 |
| - server.get('*.*', express.static(distFolder, { maxAge: '1y' })); |
63 |
| - |
64 |
| - // Step 3: handle rendering and serving using ISR handler |
65 |
| - server.get( |
66 |
| - '*', |
67 |
| - // Serve page if it exists in cache |
68 |
| - async (req, res, next) => await isr.serveFromCache(req, res, next), |
69 |
| - // Server side render the page and add to cache if needed |
70 |
| - async (req, res, next) => await isr.render(req, res, next) |
71 |
| - ); |
72 |
| - |
73 |
| - // Step 4: Comment out default angular universal handler, because it's will be handled in ISR render method |
74 |
| - // (req, res) => { |
75 |
| - // res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] }); |
76 |
| - // } |
77 |
| - |
78 |
| - return server; |
79 |
| -} |
80 |
| - |
81 |
| -function run(): void { |
82 |
| - const port = process.env['PORT'] || 4000; |
83 |
| - |
84 |
| - // Start up the Node server |
85 |
| - const server = app(); |
86 |
| - server.listen(port, () => { |
87 |
| - console.log(`Node Express server listening on http://localhost:${port}`); |
88 |
| - }); |
89 |
| -} |
90 |
| - |
91 |
| -// Webpack will replace 'require' with '__webpack_require__' |
92 |
| -// '__non_webpack_require__' is a proxy to Node 'require' |
93 |
| -// The below code is to ensure that the server is run only when not requiring the bundle. |
94 |
| -declare const __non_webpack_require__: NodeRequire; |
95 |
| -const mainModule = __non_webpack_require__.main; |
96 |
| -const moduleFilename = (mainModule && mainModule.filename) || ''; |
97 |
| -if (moduleFilename === __filename || moduleFilename.includes('iisnode')) { |
98 |
| - run(); |
99 |
| -} |
100 |
| - |
101 |
| -export * from './src/main.server'; |
| 1 | +// import { environment } from './src/environments/environment'; |
| 2 | +// import 'zone.js/dist/zone-node'; |
| 3 | +// |
| 4 | +// import { ngExpressEngine } from '@nguniversal/express-engine'; |
| 5 | +// import * as express from 'express'; |
| 6 | +// import { join } from 'path'; |
| 7 | +// |
| 8 | +// import { existsSync } from 'fs'; |
| 9 | +// |
| 10 | +// import { ISRHandler } from 'ngx-isr/server'; |
| 11 | +// import { RedisCacheHandler } from './redis-cache-handler'; |
| 12 | +// |
| 13 | +// import bootstrap from './src/main.server'; |
| 14 | +// |
| 15 | +// // The Express app is exported so that it can be used by serverless Functions. |
| 16 | +// export function app(): express.Express { |
| 17 | +// const server = express(); |
| 18 | +// const distFolder = join(process.cwd(), 'dist/docs/browser'); |
| 19 | +// const indexHtml = existsSync(join(distFolder, 'index.original.html')) |
| 20 | +// ? 'index.original.html' |
| 21 | +// : 'index'; |
| 22 | +// |
| 23 | +// const REDIS_CONNECTION_STRING = process.env['REDIS_CONNECTION_STRING'] || ''; |
| 24 | +// const INVALIDATE_TOKEN = process.env['INVALIDATE_TOKEN'] || ''; |
| 25 | +// |
| 26 | +// // Step 0 (optional): Create FileSystemCacheHandler with required options. |
| 27 | +// // const fsCacheHandler = new FileSystemCacheHandler({ |
| 28 | +// // cacheFolderPath: join(distFolder, '/cache'), |
| 29 | +// // prerenderedPagesPath: distFolder, |
| 30 | +// // addPrerenderedPagesToCache: true, |
| 31 | +// // }); |
| 32 | +// |
| 33 | +// const redisCacheHandler = REDIS_CONNECTION_STRING |
| 34 | +// ? new RedisCacheHandler({ connectionString: REDIS_CONNECTION_STRING }) |
| 35 | +// : undefined; |
| 36 | +// |
| 37 | +// // Step 1: Initialize ISRHandler |
| 38 | +// const isr = new ISRHandler({ |
| 39 | +// indexHtml, |
| 40 | +// cache: redisCacheHandler, // we can remove this field if we want to use the default InMemoryCacheHandler |
| 41 | +// invalidateSecretToken: INVALIDATE_TOKEN || 'MY_TOKEN', |
| 42 | +// enableLogging: !environment.production, |
| 43 | +// buildId: environment.buildTimestamp + '', |
| 44 | +// }); |
| 45 | +// |
| 46 | +// // Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine) |
| 47 | +// server.engine('html', ngExpressEngine({ bootstrap })); |
| 48 | +// |
| 49 | +// server.set('view engine', 'html'); |
| 50 | +// server.set('views', distFolder); |
| 51 | +// |
| 52 | +// // needed for post requests in our case we use it for the invalidation url |
| 53 | +// server.use(express.json()); |
| 54 | +// |
| 55 | +// // Example Express Rest API endpoints |
| 56 | +// // server.get('/api/**', (req, res) => { }); |
| 57 | +// |
| 58 | +// // Step 2: Add invalidation url handler |
| 59 | +// server.post('/api/invalidate', async (req, res) => await isr.invalidate(req, res)); |
| 60 | +// |
| 61 | +// // Serve static files from /browser |
| 62 | +// server.get('*.*', express.static(distFolder, { maxAge: '1y' })); |
| 63 | +// |
| 64 | +// // Step 3: handle rendering and serving using ISR handler |
| 65 | +// server.get( |
| 66 | +// '*', |
| 67 | +// // Serve page if it exists in cache |
| 68 | +// async (req, res, next) => await isr.serveFromCache(req, res, next), |
| 69 | +// // Server side render the page and add to cache if needed |
| 70 | +// async (req, res, next) => await isr.render(req, res, next) |
| 71 | +// ); |
| 72 | +// |
| 73 | +// // Step 4: Comment out default angular universal handler, because it's will be handled in ISR render method |
| 74 | +// // (req, res) => { |
| 75 | +// // res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] }); |
| 76 | +// // } |
| 77 | +// |
| 78 | +// return server; |
| 79 | +// } |
| 80 | +// |
| 81 | +// function run(): void { |
| 82 | +// const port = process.env['PORT'] || 4000; |
| 83 | +// |
| 84 | +// // Start up the Node server |
| 85 | +// const server = app(); |
| 86 | +// server.listen(port, () => { |
| 87 | +// console.log(`Node Express server listening on http://localhost:${port}`); |
| 88 | +// }); |
| 89 | +// } |
| 90 | +// |
| 91 | +// // Webpack will replace 'require' with '__webpack_require__' |
| 92 | +// // '__non_webpack_require__' is a proxy to Node 'require' |
| 93 | +// // The below code is to ensure that the server is run only when not requiring the bundle. |
| 94 | +// declare const __non_webpack_require__: NodeRequire; |
| 95 | +// const mainModule = __non_webpack_require__.main; |
| 96 | +// const moduleFilename = (mainModule && mainModule.filename) || ''; |
| 97 | +// if (moduleFilename === __filename || moduleFilename.includes('iisnode')) { |
| 98 | +// run(); |
| 99 | +// } |
| 100 | +// |
| 101 | +// export * from './src/main.server'; |
0 commit comments