Project

General

Profile

1 50428 argiro.kok
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 55971 argiro.kok
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist/server/main');
24 50428 argiro.kok
25
// Express Engine
26 55971 argiro.kok
import {ngExpressEngine, RenderOptions} from '@nguniversal/express-engine';
27 50428 argiro.kok
// Import module map for lazy loading
28
import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader';
29 57444 k.triantaf
import {REQUEST, RESPONSE} from "@nguniversal/express-engine/tokens";
30 57604 argiro.kok
import {isArray} from "util";
31 50428 argiro.kok
32
// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
33 54949 argiro.kok
// app.engine('html', ngExpressEngine({
34
//   bootstrap: AppServerModuleNgFactory,
35
//   providers: [
36
//      { provide: 'request', useFactory: () => options.req, deps: [] },
37
//     provideModuleMap(LAZY_MODULE_MAP)
38
//   ]
39
// }));
40 50428 argiro.kok
41 55271 argiro.kok
// be able to get request and get domain from there
42 57444 k.triantaf
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 54949 argiro.kok
});
64
65 50428 argiro.kok
app.set('view engine', 'html');
66
app.set('views', join(DIST_FOLDER, 'browser'));
67 57604 argiro.kok
// Allow frames from specific domains
68
app.use(function (req, res, next) {
69 58821 argiro.kok
  var XFRAME_WHITELIST = [ 'http://scoobydoo.di.uoa.gr:5000/customize-layout', 'https://beta.admin.connect.openaire.eu/customize-layout','https://admin.connect.openaire.eu/customize-layout'  ];
70 58886 k.triantaf
  let referer: string;
71 57604 argiro.kok
  if(req.headers.referer){
72 58886 k.triantaf
    referer = isArray(req.headers.referer)?req.headers.referer[0]:(<string>req.headers.referer);
73 57604 argiro.kok
    referer = referer.split("?")[0];
74
  }
75 58821 argiro.kok
  if (  referer &&  (XFRAME_WHITELIST.indexOf(referer) != -1  || referer.indexOf(".d4science.org") != -1)) {
76 57604 argiro.kok
    // res.header('X-FRAME-OPTIONS', 'allow from ' +req.headers.referer);
77
  }else {
78
    res.header('X-FRAME-OPTIONS', 'SAMEORIGIN');
79
  }
80
  next();
81
});
82 59244 argiro.kok
 // - Serve sitemap based on the host -
83
  app.get('/sitemap.xml', (req, res) => {
84
    let host = req.get("host").split(":")[0];
85
    let connectLinks= ["/","/about/learn-how","/about/learn-in-depth", "/about/faq","/search/find/communities"];
86 59991 argiro.kok
    let communityLinks= ["/","/search/find/research-outcomes?size=20","/search/advanced/research-outcomes?size=20","/participate/deposit/learn-how","/organizations", "/content"];
87 59244 argiro.kok
    let sitemap = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
88
      "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">";
89
    let urlPre = "<url>\n" +
90 59991 argiro.kok
      "    <loc>";
91
    let urlSuf = "</loc>\n" +
92 59244 argiro.kok
      "    </url>";
93
    for(let link of (host.indexOf("connect.openaire.eu") == -1 ?communityLinks:connectLinks)){
94
      sitemap += urlPre + "https://" + host + link + urlSuf;
95
     }
96
    sitemap += "\n</urlset>";
97
    res.setHeader('content-type', 'application/xml');
98
    res.send( sitemap);
99
  });
100 60016 argiro.kok
app.get('/robots.txt', (req, res) => {
101
  let host = req.get("host").split(":")[0];
102
  let robots = "";
103
  if(host.indexOf(".openaire.eu") != -1 && host.indexOf("beta.") == -1 ){
104
    //link to disallow
105
    let connectLinks= ["/about/*","/search/find/communities"];
106
    let communityLinks= ["/search/advanced/*","/participate/*",
107
      "/search/project",'/search/result', '/search/publication', '/search/dataset','/search/software','/search/other','/search/dataprovider','/search/organization','/project-report',
108
      '/search/find/publications','/search/find/datasets','/search/find/software','/search/find/other','/search/find/projects','/search/find/dataproviders','/search/find/research-outcomes'
109 50428 argiro.kok
110 60016 argiro.kok
    ];
111
112
    robots = "User-Agent: *\n"+
113
    "Crawl-delay: 30\n" +
114
      "Sitemap: /sitemap.xml\n";
115
    for(let link of (host.indexOf("connect.openaire.eu") == -1 ?connectLinks:communityLinks)){
116
      robots +=  "Disallow: " + link +  "\n";
117
    }
118
  }else{
119
    robots = "User-Agent: *\n" +
120
      "Disallow: /\n"
121
  }
122
  res.setHeader('content-type', 'text/plain');
123
  res.send( robots);
124
});
125
126 50428 argiro.kok
/* - Example Express Rest API endpoints -
127
  app.get('/api/**', (req, res) => { });
128
*/
129
130
// Server static files from /browser
131
app.get('*.*', express.static(join(DIST_FOLDER, 'browser'), {
132
  maxAge: '1y'
133
}));
134
135
// ALl regular routes use the Universal engine
136
app.get('*', (req, res) => {
137
  res.render('index', { req });
138
});
139
140
// Start up the Node server
141
app.listen(PORT, () => {
142
  console.log(`Node Express server listening on http://localhost:${PORT}`);
143
});