Project

General

Profile

1
import 'zone.js/dist/zone-node';
2
import 'reflect-metadata';
3
import { renderModuleFactory } from '@angular/platform-server';
4
import { enableProdMode } from '@angular/core';
5

    
6
import * as express from 'express';
7
import { join } from 'path';
8
import { readFileSync } from 'fs';
9

    
10
// Faster server renders w/ Prod mode (dev mode never needed)
11
enableProdMode();
12

    
13
// Express server
14
const app = express();
15

    
16
const PORT = process.env.PORT || 4000;
17
const DIST_FOLDER = join(process.cwd(), 'dist');
18

    
19
// Our index.html we'll use as our template
20
const template = readFileSync(join(DIST_FOLDER, 'browser', 'index.html')).toString();
21

    
22
// * NOTE :: leave this as require() since this file is built Dynamically from webpack
23
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist/server/main');
24

    
25
// Express Engine
26
import {ngExpressEngine, RenderOptions} from '@nguniversal/express-engine';
27
// Import module map for lazy loading
28
import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader';
29
import {REQUEST, RESPONSE} from "@nguniversal/express-engine/tokens";
30
import {isArray} from "util";
31

    
32
// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
33
// app.engine('html', ngExpressEngine({
34
//   bootstrap: AppServerModuleNgFactory,
35
//   providers: [
36
//      { provide: 'request', useFactory: () => options.req, deps: [] },
37
//     provideModuleMap(LAZY_MODULE_MAP)
38
//   ]
39
// }));
40

    
41
// be able to get request and get domain from there
42

    
43
app.engine('html', (_, options: any, callback) => {
44
  const opts = {
45
    document: template,
46
    url: options.req.url,
47
    extraProviders: [
48
      provideModuleMap(LAZY_MODULE_MAP),
49
      {
50
        provide: REQUEST,
51
        useValue: options.req
52
      },
53
      {
54
        provide: RESPONSE,
55
        useValue: options.req.res,
56
      },
57
    ]
58
  };
59

    
60
  renderModuleFactory(AppServerModuleNgFactory, opts)
61
    .then(html => callback(null, html)
62
    );
63
});
64

    
65
app.set('view engine', 'html');
66
app.set('views', join(DIST_FOLDER, 'browser'));
67
// Allow frames from specific domains
68
app.use(function (req, res, next) {
69
  var XFRAME_WHITELIST = [ 'http://scoobydoo.di.uoa.gr/frames.html', 'http://scoobydoo.di.uoa.gr:5000/customize-layout', 'https://beta.admin.connect.openaire.eu/customize-layout','https://admin.connect.openaire.eu/customize-layout'  ];
70
  let referer:string;
71
  if(req.headers.referer){
72
    referer = isArray(req.headers.referer)?req.headers.referer[0]:req.headers.referer;
73
    referer = referer.split("?")[0];
74
  }
75
  if (  referer &&  XFRAME_WHITELIST.indexOf(referer) != -1 ) {
76
    // res.header('X-FRAME-OPTIONS', 'allow from ' +req.headers.referer);
77
  }else {
78
    res.header('X-FRAME-OPTIONS', 'SAMEORIGIN');
79
  }
80
  next();
81
});
82

    
83
/* - Example Express Rest API endpoints -
84
  app.get('/api/**', (req, res) => { });
85
*/
86

    
87
// Server static files from /browser
88
app.get('*.*', express.static(join(DIST_FOLDER, 'browser'), {
89
  maxAge: '1y'
90
}));
91

    
92
// ALl regular routes use the Universal engine
93
app.get('*', (req, res) => {
94
  res.render('index', { req });
95
});
96

    
97
// Start up the Node server
98
app.listen(PORT, () => {
99
  console.log(`Node Express server listening on http://localhost:${PORT}`);
100
});
(5-5/8)