Project

General

Profile

1
import 'zone.js/dist/zone-node';
2

    
3
import { ngExpressEngine } from '@nguniversal/express-engine';
4
import * as express from 'express';
5
import * as compression from 'compression';
6
import { join } from 'path';
7

    
8
import { AppServerModule } from './src/main.server';
9
import { APP_BASE_HREF } from '@angular/common';
10
import { existsSync } from 'fs';
11
import {REQUEST, RESPONSE} from "./src/app/openaireLibrary/utils/tokens";
12

    
13
// The Express app is exported so that it can be used by serverless Functions.
14
export function app() {
15
  const server = express();
16
  server.use(compression());
17
  const distFolder = join(process.cwd(), 'dist/monitor/browser');
18
  const indexHtml = existsSync(join(distFolder, 'index.original.html')) ? 'index.original.html' : 'index';
19

    
20
  // Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
21
  server.engine('html', ngExpressEngine({
22
    bootstrap: AppServerModule,
23
  }));
24

    
25
  server.set('view engine', 'html');
26
  server.set('views', distFolder);
27

    
28
  // Example Express Rest API endpoints
29
  // server.get('/api/**', (req, res) => { });
30
  // Serve static files from /browser
31
  server.get('*.*', express.static(distFolder, {
32
    maxAge: '1y'
33
  }));
34

    
35
  // All regular routes use the Universal engine
36
  server.get('*', (req, res) => {
37
    res.render(indexHtml, {
38
        req, providers: [
39
          {
40
            provide: APP_BASE_HREF,
41
            useValue: req.baseUrl
42
          },
43
          {
44
            provide: REQUEST, useValue: (req)
45
          },
46
          {
47
            provide: RESPONSE, useValue: (res)
48
          }
49
        ]
50
      }
51
    );
52
  });
53

    
54
  return server;
55
}
56

    
57
function run() {
58
  const port = process.env.PORT || 4000;
59

    
60
  // Start up the Node server
61
  const server = app();
62
  server.listen(port, () => {
63
    console.log(`Node Express server listening on http://localhost:${port}`);
64
  });
65
}
66

    
67
// Webpack will replace 'require' with '__webpack_require__'
68
// '__non_webpack_require__' is a proxy to Node 'require'
69
// The below code is to ensure that the server is run only when not requiring the bundle.
70
declare const __non_webpack_require__: NodeRequire;
71
const mainModule = __non_webpack_require__.main;
72
const moduleFilename = mainModule && mainModule.filename || '';
73
if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
74
  run();
75
}
76

    
77
export * from './src/main.server';
(6-6/7)