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 {Prometheus} from "./prometheus";
12
import {Counter} from "prom-client";
13
import {REQUEST, RESPONSE} from "./src/app/openaireLibrary/utils/tokens";
14

    
15
// The Express app is exported so that it can be used by serverless Functions.
16
export function app() {
17
  const server = express();
18
  server.use(compression());
19
  const distFolder = join(process.cwd(), 'dist/aggregator/browser');
20
  const indexHtml = existsSync(join(distFolder, 'index.original.html')) ? 'index.original.html' : 'index';
21
  
22
  const prometheus: Prometheus = new Prometheus();
23
  
24
  // Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
25
  server.engine('html', ngExpressEngine({
26
    bootstrap: AppServerModule,
27
  }));
28

    
29
  server.set('view engine', 'html');
30
  server.set('views', distFolder);
31

    
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
  
39
  server.get('/metrics', (req, res) => {
40
    res.set('Content-Type', prometheus.register.contentType);
41
    res.end(prometheus.register.metrics());
42
  });
43
  
44
  // All regular routes use the Universal engine
45
  server.get('*', (req, res) => {
46
    let start = new Date();
47
    let counter: Counter = prometheus.counters.get(req.path);
48
    if(counter !== undefined) {
49
      counter.inc(1, new Date());
50
      res.render(indexHtml, {
51
        req, providers: [
52
          {
53
            provide: APP_BASE_HREF,
54
            useValue: req.baseUrl
55
          },
56
          {
57
            provide: REQUEST, useValue: (req)
58
          },
59
          {
60
            provide: RESPONSE, useValue: (res)
61
          }
62
        ]
63
      });
64
      // event triggers when express is done sending response
65
      res.on('finish', function() {
66
        console.log(new Date().getTime() - start.getTime());
67
      });
68
    } else {
69
      res.render(indexHtml, {
70
        req, providers: [
71
          {
72
            provide: APP_BASE_HREF,
73
            useValue: req.baseUrl
74
          },
75
          {
76
            provide: REQUEST, useValue: (req)
77
          },
78
          {
79
            provide: RESPONSE, useValue: (res)
80
          }
81
        ]
82
      });
83
    }
84
  });
85

    
86
  return server;
87
}
88

    
89
function run() {
90
  const port = process.env.PORT || 4000;
91

    
92
  // Start up the Node server
93
  const server = app();
94
  server.listen(port, () => {
95
    console.log(`Node Express server listening on http://localhost:${port}`);
96
  });
97
}
98

    
99
// Webpack will replace 'require' with '__webpack_require__'
100
// '__non_webpack_require__' is a proxy to Node 'require'
101
// The below code is to ensure that the server is run only when not requiring the bundle.
102
declare const __non_webpack_require__: NodeRequire;
103
const mainModule = __non_webpack_require__.main;
104
const moduleFilename = mainModule && mainModule.filename || '';
105
if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
106
  run();
107
}
108

    
109
export * from './src/main.server';
(7-7/8)