1 |
61074
|
k.triantaf
|
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 * as prom from "prom-client";
|
12 |
|
|
import {routes} from "./routes";
|
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/explore/browser');
|
20 |
|
|
const indexHtml = existsSync(join(distFolder, 'index.original.html')) ? 'index.original.html' : 'index';
|
21 |
|
|
|
22 |
|
|
const register = new prom.Registry();
|
23 |
|
|
|
24 |
|
|
const responses = new prom.Counter({
|
25 |
|
|
name: 'portal_http_responses_total',
|
26 |
|
|
help: 'A counter for portal response codes for every existed or not existed pages.',
|
27 |
|
|
labelNames: ['route', 'code'],
|
28 |
|
|
registers: [register]
|
29 |
|
|
});
|
30 |
|
|
|
31 |
|
|
const histogram = new prom.Histogram({
|
32 |
|
|
name: 'portal_http_request_duration_seconds',
|
33 |
|
|
help: 'A Histogram for a portal. Providing information about page visits and load latency in seconds.',
|
34 |
|
|
labelNames: ['route'],
|
35 |
|
|
registers: [register],
|
36 |
|
|
buckets: [0.1, 0.2, 0.5, 1, 2]
|
37 |
|
|
});
|
38 |
|
|
|
39 |
|
|
// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
|
40 |
|
|
server.engine('html', ngExpressEngine({
|
41 |
|
|
bootstrap: AppServerModule,
|
42 |
|
|
}));
|
43 |
|
|
|
44 |
|
|
server.set('view engine', 'html');
|
45 |
|
|
server.set('views', distFolder);
|
46 |
|
|
|
47 |
|
|
// Example Express Rest API endpoints
|
48 |
|
|
// server.get('/api/**', (req, res) => { });
|
49 |
|
|
// Serve static files from /browser
|
50 |
|
|
server.get('*.*', express.static(distFolder, {
|
51 |
|
|
maxAge: '1y'
|
52 |
|
|
}));
|
53 |
|
|
|
54 |
|
|
server.get('/metrics', (req, res) => {
|
55 |
|
|
res.set('Content-Type', register.contentType);
|
56 |
|
|
res.end(register.metrics());
|
57 |
|
|
});
|
58 |
|
|
|
59 |
|
|
// All regular routes use the Universal engine
|
60 |
|
|
server.get('*', (req, res) => {
|
61 |
|
|
if (routes.indexOf(req.path) !== -1) {
|
62 |
|
|
const end = histogram.startTimer({route: req.path});
|
63 |
|
|
res.render(indexHtml, {
|
64 |
|
|
req, providers: [
|
65 |
|
|
{
|
66 |
|
|
provide: APP_BASE_HREF,
|
67 |
|
|
useValue: req.baseUrl
|
68 |
|
|
},
|
69 |
|
|
{
|
70 |
|
|
provide: REQUEST, useValue: (req)
|
71 |
|
|
},
|
72 |
|
|
{
|
73 |
|
|
provide: RESPONSE, useValue: (res)
|
74 |
|
|
}
|
75 |
|
|
]
|
76 |
|
|
}
|
77 |
|
|
);
|
78 |
|
|
res.on('finish', function () {
|
79 |
|
|
responses.inc({route: req.path, code: res.statusCode});
|
80 |
|
|
end();
|
81 |
|
|
});
|
82 |
|
|
} else {
|
83 |
|
|
res.render(indexHtml, {
|
84 |
|
|
req, providers: [
|
85 |
|
|
{
|
86 |
|
|
provide: APP_BASE_HREF,
|
87 |
|
|
useValue: req.baseUrl
|
88 |
|
|
},
|
89 |
|
|
{
|
90 |
|
|
provide: REQUEST, useValue: (req)
|
91 |
|
|
},
|
92 |
|
|
{
|
93 |
|
|
provide: RESPONSE, useValue: (res)
|
94 |
|
|
}
|
95 |
|
|
]
|
96 |
|
|
}
|
97 |
|
|
);
|
98 |
|
|
res.on('finish', function () {
|
99 |
|
|
responses.inc({route: '**', code: res.statusCode});
|
100 |
|
|
});
|
101 |
|
|
}
|
102 |
|
|
});
|
103 |
|
|
|
104 |
|
|
return server;
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
function run() {
|
108 |
|
|
const port = process.env.PORT || 4000;
|
109 |
|
|
|
110 |
|
|
// Start up the Node server
|
111 |
|
|
const server = app();
|
112 |
|
|
server.listen(port, () => {
|
113 |
|
|
console.log(`Node Express server listening on http://localhost:${port}`);
|
114 |
|
|
});
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
// Webpack will replace 'require' with '__webpack_require__'
|
118 |
|
|
// '__non_webpack_require__' is a proxy to Node 'require'
|
119 |
|
|
// The below code is to ensure that the server is run only when not requiring the bundle.
|
120 |
|
|
declare const __non_webpack_require__: NodeRequire;
|
121 |
|
|
const mainModule = __non_webpack_require__.main;
|
122 |
|
|
const moduleFilename = mainModule && mainModule.filename || '';
|
123 |
|
|
if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
|
124 |
|
|
run();
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
export * from './src/main.server';
|